Skip to main content

What is an exception

An exception is an event that occurs while the program is running, when an error happens and the normal execution of the code is interrupted. In other words, an exception is a "signal" about an error situation that the JavaScript engine throws (throw) and that you can catch (catch).

Theory

TL;DR

  • An exception is the moment an error is thrown, while Error is the object holding information about what went wrong.
  • An exception interrupts the normal flow of execution and starts the search for the nearest try...catch.
  • The mechanism rests on four keywords: try, throw, catch, finally.
  • Exceptions are either automatic (thrown by the engine) or user defined (thrown by the developer with throw).
  • If nobody catches an exception, it bubbles up the call stack and the program terminates with an error.

Quick example

javascript
function divide(a, b) { if (b === 0) { throw new Error("Division by zero is not allowed"); } return a / b; } try { divide(5, 0); // here the exception is thrown } catch (err) { console.log("Caught exception:", err.message); }

Definition

An exception is an event that arises while the program is running, when an error happens and the normal execution of the code is interrupted.

Put differently, an exception is a "signal" about an error situation that the JavaScript engine throws (throw) and that can be caught (catch).

The difference between error and exception

  • Error is an object that carries information about what went wrong.
  • Exception is the moment when that error object was thrown during execution.

Put differently: Error is the data, Exception is the event of throwing that data.

How an exception actually arises

Let us walk through the example above step by step:

  1. The divide function runs.
  2. The condition b === 0 is true, and the engine throws an error object.
  3. That event is the exception.
  4. JavaScript starts looking for the nearest try...catch.
  5. The catch block intercepts the exception, and the program keeps running.

The exception mechanism: try / throw / catch / finally

KeywordWhat it does
tryThe block where an error may happen
throwThrows an exception
catchIntercepts the exception and handles it
finallyAlways runs, even if there was an error

Example:

javascript
try { throw new Error("Network error"); } catch (error) { console.log("Caught exception:", error.message); } finally { console.log("This code always runs"); }

Two kinds of exceptions

  1. Automatic (thrown by the engine). They appear when the engine itself detects an error:

    javascript
    console.log(a); // ReferenceError, the exception is thrown automatically
  2. User defined (explicit). When the programmer throws the exception themselves:

    javascript
    throw new Error("Something went wrong");

What happens if an exception is not caught

If an exception is not intercepted (there is no catch block), it bubbles up the call stack. If in the end nobody caught it, the program terminates with an error:

javascript
function a() { b(); } function b() { throw new Error("Failure"); } a(); // the error will terminate execution

In the browser such an exception ends up in the console and, if you want, in the global window.onerror handler; in Node.js the uncaughtException event terminates the process by default.

Common mistakes

  • Confusing Error and an exception. Creating new Error("...") on its own interrupts nothing: it becomes an exception only after throw.
  • An empty catch. A catch {} block with no handling hides the problem: the program runs on in a broken state and you can no longer see the cause.
  • Expecting try...catch to catch an asynchronous error. An exception thrown inside a setTimeout callback or in an unhandled promise will not reach the surrounding try...catch; promises need await inside try, or .catch().
  • Throwing something that is not an error object. throw "text" is technically allowed, but then you have neither message nor stack; always throw new Error(...) or a subclass of it.
  • Forgetting that finally runs even after return. A return inside finally overwrites the value returned from try and can silently swallow the exception.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.