liveBook · Manning

Error propagation

As explained in JavaScript in Depth

The movement of a thrown error through nested execution contexts until a handler intercepts it or the runtime reports it as uncaught.

A thrown error moves up the active synchronous call stack until a catch handler intercepts it or the runtime reports it as uncaught. Calls above the throw site stop executing, so recovery depends on where a handler appears. 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 the failure from level3 through level2 and level1 to observe how unwinding skips statements after the failing call. section 7.5

Handlers can recover locally, clean up, or add context before rethrowing. A transaction handler can roll back and throw a new error with the original error as its cause, preserving both operational context and the underlying failure. section 7.5

Avoid the propagation traps that make failures harder to recover or diagnose:

  • Treat finally as part of the propagation path: returning from it suppresses a pending error, while throwing from it replaces the original error. section 7.5
  • Use throw, try, and catch for exceptional conditions rather than implementing ordinary branching with exceptions; use return, break, or continue for control flow. section 7.5
  • Do not expect a surrounding synchronous try block to catch an error thrown later by setTimeout; asynchronous scheduling breaks the original call stack. section 7.5
  • Uncaught asynchronous failures require host-specific handling, such as browser error and unhandledrejection events or Node.js uncaughtException and unhandledRejection events. section 7.5

A cleanup block can erase the failure

A function that throws inside try but returns from finally produces the return value instead of propagating the error. A finally block that throws a second error propagates the second error instead, leaving the original failure unavailable to ordinary recovery.

How JavaScript in Depth covers it

How deep it goes
Explains synchronous unwinding, finally interference, stack boundaries, and host handlers, but does not develop streaming-model behavior.
Examples
Uses nested functions, transaction rollback, finally suppression, forEach search, setTimeout, browser events, and Node.js process handlers.
What it assumes
Assumes JavaScript functions and asynchronous scheduling while teaching call-stack unwinding and handler placement through concrete examples.
What you can do after
Diagnose/debug failures by tracing stack boundaries, preserving causes, avoiding suppression, and selecting browser or Node.js handlers.
How it connects
Connects error propagation with exception handling, call stacks, finally blocks, transactions, asynchronous programming, promises, and event loops.

Also explained in

Compare how 5 books explain error propagation →

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.