CKTIS / JavaScript
throw — throwing an exception
The throw statement raises an exception and interrupts normal execution. A catch clause can receive the thrown value; no line break is allowed between throw and its expression.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
throw expression;throw is a reserved keyword that starts a statement, not an expression. The statement throw expression; evaluates the expression and throws its resulting value as an exception. Any JavaScript value can be thrown; for errors, prefer an Error object with a message.
An exception interrupts normal execution and propagates through synchronous function calls to an enclosing catch. Without a handler, it remains uncaught. An exception that escapes an async function rejects the promise it returns. A try/catch around the call alone does not catch that rejection. To catch it in catch, await the promise inside the try block.
Blocks named finally execute as control leaves protected blocks; a return or another throw in finally can replace the earlier exception.
The expression is required: throw; alone is invalid. A line break must not occur between throw and the start of the expression—automatic semicolon insertion does not fix this. To rethrow a caught exception, use throw error; inside catch (error).
Examples
function requirePositive(value) {
if (typeof value !== "number" || !(value > 0)) {
throw new Error("Expected a positive number");
}
return value;
}
try {
requirePositive(0);
console.log("Not reached");
} catch (error) {
console.log(error.message);
}Result
Expected a positive number
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.