Skip to main content

What the throw statement does

throw is a statement that interrupts the execution of the current code and throws an exception: an error or any other object. It turns an ordinary value into an error event that try...catch then catches, and if there is nothing to catch it, the exception bubbles up the stack and terminates the program.

Theory

TL;DR

  • throw expression; forcibly raises an exception and cuts the current block short.
  • You can throw any value, but the right thing to throw is an Error or a subclass of it.
  • Control passes to the nearest catch; without one the exception stays uncaught.
  • Code written after throw in the same block will never run.
  • throw is the main tool for validating function input.

Quick example

javascript
try { throw new Error("Something went wrong during execution"); } catch (err) { console.log("Caught:", err.message); } // Caught: Something went wrong during execution

Syntax and what exactly you can throw

The syntax is as short as it gets:

javascript
throw expression;

Here expression can be any JavaScript value: an object, a number, a string, a boolean and so on. Most often, though, what gets thrown is an instance of the Error class.

The simplest use:

javascript
throw "Something went wrong";

In the console:

text
Uncaught Something went wrong

The program stops immediately here, because the exception was not caught.

You can also throw a plain object:

javascript
try { throw { message: "Network error", code: 500 }; } catch (e) { console.log(e.message, e.code); } // Network error 500

But this is not recommended: always prefer the standard error classes (Error, TypeError, RangeError and so on) to keep readability, compatibility and the call stack.

throw together with try / catch / finally

When throw fires inside try, this is what happens:

  1. throw creates an exception, usually an Error object.
  2. Execution of the try block is interrupted at that line.
  3. Control is handed to catch.
  4. The error is handled and the program continues.
javascript
try { // throw the exception throw new Error("Failure"); } catch (e) { // catch the exception console.log("Caught:", e.message); } finally { console.log("This block always runs"); }

The finally block runs in either case: whether there was an exception or not.

Validating input with throw

The most common practical scenario is validating function arguments:

javascript
function divide(a, b) { if (typeof a !== "number" || typeof b !== "number") { throw new TypeError("Both arguments must be numbers"); } if (b === 0) { throw new Error("Division by zero is not allowed"); } return a / b; } try { console.log(divide(5, 0)); } catch (e) { console.error("Error:", e.message); } // Error: Division by zero is not allowed

Note the choice of class: a wrong argument type naturally calls for TypeError, a value outside the allowed range for RangeError, and a domain failure for your own subclass of Error.

Code after throw does not run

throw acts as an immediate exit from the block:

javascript
console.log("before throw"); throw new Error("failure"); console.log("after throw"); // will not run

After throw, execution of the current block stops, and control is passed to the error handler, if there is one.

Summary

ItemDescription
PurposeForcibly throws an exception (an error)
Value typeAny data type can be thrown (usually Error)
EffectInterrupts execution of the current block
Used withtry...catch for error handling
After throwThe code that follows does not run

Common mistakes

  • Throwing a string instead of an Error. throw "text" deprives you of stack and name, and monitoring tools will not report such an exception properly.
  • Writing code after throw. Everything that follows it in the same block is dead code that will never execute.
  • Confusing throw and return. return hands a value back to the caller, throw unwinds the stack until it finds a catch.
  • Throwing inside an asynchronous callback. A throw in a setTimeout callback will not reach the surrounding try...catch, because that block has already finished.
  • Swallowing the exception with an empty catch. Once you catch it, either handle it or rethrow with throw err, otherwise the cause of the failure disappears.

Short Answer

Interview ready
Premium

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