liveBook · Manning

Throw

As explained in JavaScript in Depth

A JavaScript mechanism that interrupts normal control flow by propagating a value up the call stack as an exception.

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" };  #B

Compare 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();  #B

Trace 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

How JavaScript in Depth covers it

How deep it goes
Explains value propagation, stack unwinding, catch boundaries, finally overrides, asynchronous limits, and host-specific uncaught behavior.
Examples
Uses arbitrary thrown values, nested call stacks, transaction rollback, finally overrides, array traversal, timers, browser events, and Node.js handlers.
What you can do after
Diagnose control-flow failures by tracing stack unwinding, catch scope, asynchronous boundaries, and runtime-specific uncaught handling.
How it connects
Connects throw with Error objects, call stacks, try-catch, finally, asynchronous callbacks, error causes, and uncaught exception handling.

Also explained in

Compare how 4 books explain throw →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.