Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท JSON, Errors and Reflection

The Error object

Reference for the JavaScript Error object: create, throw and catch errors, read name, message and stack, subclass Error and use instanceof to tell errors apart.

Error is the base type for runtime errors. An error object carries a message describing what went wrong, a name naming the kind of error, and (in practice in every engine) a stack string that records where it was created. You throw an error to abort normal flow and catch it to recover.

The language and the platform throw instances of Error subclasses such as TypeError and RangeError. You throw your own with throw new Error("..."), or by extending Error to make a more specific type.

JavaScript
try {
  throw new Error("Something broke");
} catch (error) {
  console.log(error.name, error.message); // Error Something broke
}

Try it

JS Creating and inspecting errors

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS A custom error class

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
new Error()
new Error(message)
new Error(message, options)
Error(message, options)
Name Description
message Optional human-readable description. Converted to a string unless undefined.
options Optional object. Its cause property, if present, becomes the error's cause (see Error cause).

Properties of instances:

Property Description
name The error kind, inherited from the prototype: "Error" for Error. Set it yourself in subclasses.
message The message passed to the constructor, or "". An own, non-enumerable property.
cause Only present when given through options.
stack A string describing the call stack. Not part of the standard, and its format differs between engines, so do not parse it.

error.toString() returns "name: message", or just the name when the message is empty.

Exceptions: the constructor does not throw for ordinary arguments.

Usage notes

  • new is optional. Error("x") behaves like new Error("x"), although new is clearer.
  • Throw error objects, not strings. Only error objects carry a stack trace, and catch code can check instanceof. Anything can be thrown, so a catch block should not assume it received an Error.
  • message is non-enumerable, so JSON.stringify(error) is {} and spreading an error loses it. Copy the fields you need explicitly.
  • Set name in subclasses (this.name = "ValidationError"), otherwise the class prints as Error. Add fields such as field or code for programmatic handling.
  • Match by type, not by message. Message text is meant for people and can change; use instanceof or a code property.
  • Rethrow what you cannot handle. Catch, check the type, and throw error again for unknown cases.
  • Stack traces are created when the error is constructed, not when it is thrown.
  • Browsers also provide DOMException for platform failures such as an aborted fetch; check its name property.

Browser support

Error is supported everywhere. The cause option is newer than the rest, but it is available in current browsers and runtimes.