A throw interrupts normal JavaScript execution and sends a value up the call stack for handling. It signals that the current code cannot continue. JavaScript permits any value—an Error object, string, number, null, undefined, or custom object—but common practice favors Error objects because they carry useful debugging context. section 7.2
throw new Error("Error object"); #A
throw "just a string"; #B
throw 42; #B
throw null; #B
throw undefined; #B
throw { custom: "object" }; #BCompare Error objects with arbitrary throwable values, including strings, primitives, null, undefined, and custom objects. section 7.2
The throw keyword does not create an error; it changes control flow. The engine stops the current path, unwinds stack frames one by one, and searches for a catch handler. Code after the throw in those frames does not run. If no handler exists, the value becomes uncaught and the host runtime decides what happens. section 7.5
function level3() {
throw new Error("Origin point");
}
function level2() {
level3(); #A
console.log("Never executed");
}
function level1() {
level2(); #A
console.log("Never executed");
}
level1(); #BTrace an exception through nested function calls and observe how statements after each call become unreachable. section 7.5
Several control-flow details make throw easy to misuse:
- A nearer catch handler intercepts the propagated value, preventing an outer handler from running. section 7.5
- A finally block can suppress an earlier throw with return or replace it with a new throw. section 7.5
- Throwing inside a synchronous callback does not reach a try-catch that has already returned; asynchronous callbacks need runtime-specific handling. section 7.5
Preserve the cause while rolling back
A transaction can catch a failure, roll back its changes, and rethrow a new Error with the original value as its cause. This preserves a higher-level message without discarding lower-level debugging context. section 7.5



