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.
try { throw new Error("Something broke"); } catch (error) { console.log(error.name, error.message); // Error Something broke }
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
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
newis optional.Error("x")behaves likenew Error("x"), althoughnewis clearer.- Throw error objects, not strings. Only error objects carry a stack trace, and
catchcode can checkinstanceof. Anything can be thrown, so acatchblock should not assume it received anError. messageis non-enumerable, soJSON.stringify(error)is{}and spreading an error loses it. Copy the fields you need explicitly.- Set
namein subclasses (this.name = "ValidationError"), otherwise the class prints asError. Add fields such asfieldorcodefor programmatic handling. - Match by type, not by message. Message text is meant for people and can change; use
instanceofor acodeproperty. - Rethrow what you cannot handle. Catch, check the type, and
throw erroragain for unknown cases. - Stack traces are created when the error is constructed, not when it is thrown.
- Browsers also provide
DOMExceptionfor platform failures such as an aborted fetch; check itsnameproperty.
Browser support
Error is supported everywhere. The cause option is newer than the rest, but it is available in current browsers and runtimes.
Related
- Error cause, error types,
AggregateError,throw,try...catch - Lessons: Errors, Throwing errors