The Error constructor creates an error object. Instances of Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. See below for standard built-in error types.
Syntax
new Error([message[, fileName[, lineNumber]]])
Parameters
message- A human-readable description of the error.
fileNameOptional- The value for the
fileNameproperty on the createdErrorobject. Defaults to the name of the file containing the code that called theError()constructor. lineNumberOptional- The value for the
lineNumberproperty on the createdErrorobject. Defaults to the line number containing theError()constructor invocation.
Description
Runtime errors result in new Error objects being created and thrown.
This page documents the use of the Error object itself and its use as a constructor function. For a list of properties and methods inherited by Error instances, see Error.prototype.
Used as a function
When Error is used like a function -- without new, it will return an Error object. Therefore, a mere call to Error will produce the same output that constructing an Error object via the new keyword would.
// this:
const x = Error('I was created using a function call!');
// has the same functionality as this:
const y = new Error('I was constructed via the "new" keyword!');
Error types
Besides the generic Error constructor, there are seven other core error constructors in JavaScript. For client-side exceptions, see Exception handling statements.
EvalError- Creates an instance representing an error that occurs regarding the global function
eval(). InternalError- Creates an instance representing an error that occurs when an internal error in the JavaScript engine is thrown. E.g. "too much recursion".
RangeError- Creates an instance representing an error that occurs when a numeric variable or parameter is outside of its valid range.
ReferenceError- Creates an instance representing an error that occurs when de-referencing an invalid reference.
SyntaxError- Creates an instance representing a syntax error that occurs while parsing code in
eval(). TypeError- Creates an instance representing an error that occurs when a variable or parameter is not of a valid type.
URIError- Creates an instance representing an error that occurs when
encodeURI()ordecodeURI()are passed invalid parameters.
Properties
Error.prototype- Allows the addition of properties to
Errorinstances.
Methods
The global Error object contains no methods of its own, however, it does inherit some methods from the prototype chain.
Error instances
All Error instances and instances of non-generic errors inherit from Error.prototype. As with all constructor functions, you can use the prototype of the constructor to add properties or methods to all instances created with that constructor.
Properties
Standard properties
Error.prototype.constructor- Specifies the function that created an instance's prototype.
Error.prototype.message- Error message.
Error.prototype.name- Error name.
Vendor-specific extensions
Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
Microsoft
Error.prototype.description- Error description. Similar to
message. Error.prototype.number- Error number.
Mozilla
Error.prototype.fileName- Path to file that raised this error.
Error.prototype.lineNumber- Line number in file that raised this error.
Error.prototype.columnNumber- Column number in line that raised this error.
Error.prototype.stack- Stack trace.
Methods
Error.prototype.toSource()- Returns a string containing the source of the specified
Errorobject; you can use this value to create a new object. Overrides theObject.prototype.toSource()method. Error.prototype.toString()- Returns a string representing the specified object. Overrides the
Object.prototype.toString()method.
Examples
Throwing a generic error
Usually you create an Error object with the intention of raising it using the throw keyword. You can handle the error using the try...catch construct:
try {
throw new Error('Whoops!');
} catch (e) {
console.error(e.name + ': ' + e.message);
}
Handling a specific error
You can choose to handle only specific error types by testing the error type with the error's constructor property or, if you're writing for modern JavaScript engines, instanceof keyword:
try {
foo.bar();
} catch (e) {
if (e instanceof EvalError) {
console.error(e.name + ': ' + e.message);
} else if (e instanceof RangeError) {
console.error(e.name + ': ' + e.message);
}
// ... etc
}
Custom Error Types
You might want to define your own error types deriving from Error to be able to throw new MyError() and use instanceof MyError to check the kind of error in the exception handler. This results in cleaner and more consistent error handling code. See "What's a good way to extend Error in JavaScript?" on StackOverflow for an in-depth discussion.
ES6 Custom Error Class
Versions of Babel prior to 7 can handle CustomError class methods, but only when they are declared with Object.defineProperty(). Otherwise, old versions of Babel and other transpilers will not correctly handle the following code without additional configuration.
Some browsers include the CustomError constructor in the stack trace when using ES2015 classes.
class CustomError extends Error {
constructor(foo = 'bar', ...params) {
// Pass remaining arguments (including vendor specific ones) to parent constructor
super(...params);
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, CustomError);
}
this.name = 'CustomError';
// Custom debugging information
this.foo = foo;
this.date = new Date();
}
}
try {
throw new CustomError('baz', 'bazMessage');
} catch(e){
console.error(e.name); //CustomError
console.error(e.foo); //baz
console.error(e.message); //bazMessage
console.error(e.stack); //stacktrace
}
ES5 Custom Error Object
All browsers include the CustomError constructor in the stack trace when using a prototypal declaration.
function CustomError(foo, message, fileName, lineNumber) {
var instance = new Error(message, fileName, lineNumber);
instance.name = 'CustomError';
instance.foo = foo;
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
if (Error.captureStackTrace) {
Error.captureStackTrace(instance, CustomError);
}
return instance;
}
CustomError.prototype = Object.create(Error.prototype, {
constructor: {
value: Error,
enumerable: false,
writable: true,
configurable: true
}
});
if (Object.setPrototypeOf){
Object.setPrototypeOf(CustomError, Error);
} else {
CustomError.__proto__ = Error;
}
try {
throw new CustomError('baz', 'bazMessage');
} catch(e){
console.error(e.name); //CustomError
console.error(e.foo); //baz
console.error(e.message); //bazMessage
}
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript Latest Draft (ECMA-262) The definition of 'Error' in that specification. |
Draft | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Error' in that specification. |
Standard | |
| ECMAScript 5.1 (ECMA-262) The definition of 'Error' in that specification. |
Standard | |
| ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.1. |
Browser compatibility
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Error | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 6 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
columnNumber | Chrome No support No | Edge No support No | Firefox Full support 1 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android Full support 4 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
fileName | Chrome No support No | Edge No support No | Firefox Full support 1 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android Full support 4 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
lineNumber | Chrome No support No | Edge No support No | Firefox Full support 1 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android Full support 4 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
message | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 6 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
name | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 6 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
prototype | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 6 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
stack | Chrome Full support 3 | Edge Full support 12 | Firefox Full support 1 | IE Full support 10 | Opera Full support Yes | Safari Full support 6 | WebView Android Full support ≤37 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support 6 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
toSource | Chrome No support No | Edge No support No | Firefox Full support 1 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android Full support 4 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
toString | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 6 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.