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.
new Error([message[, fileName[, lineNumber]]])
message
fileName
fileName
property on the created Error
object. Defaults to the name of the file containing the code that called the Error()
constructor.lineNumber
lineNumber
property on the created Error
object. Defaults to the line number containing the Error()
constructor invocation.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
.
Besides the generic Error
constructor, there are seven other core error constructors in JavaScript. For client-side exceptions, see Exception Handling Statements.
EvalError
eval()
.InternalError
RangeError
ReferenceError
SyntaxError
eval()
.TypeError
URIError
encodeURI()
or decodeURI()
are passed invalid parameters.Error.prototype
Error
instances.The global Error
object contains no methods of its own, however, it does inherit some methods from the prototype chain.
Error.prototype.constructor
Error.prototype.message
Error.prototype.name
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.
Error.prototype.description
message
.Error.prototype.number
Error.prototype.fileName
Error.prototype.lineNumber
Error.prototype.columnNumber
Error.prototype.stack
Error.prototype.toSource()
Error
object; you can use this value to create a new object. Overrides the Object.prototype.toSource()
method.Error.prototype.toString()
Object.prototype.toString()
method.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.log(e.name + ': ' + e.message); }
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.log(e.name + ': ' + e.message); } else if (e instanceof RangeError) { console.log(e.name + ': ' + e.message); } // ... etc }
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.
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 ES6 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); } // Custom debugging information this.foo = foo; this.date = new Date(); } } try { throw new CustomError('baz', 'bazMessage'); } catch(e){ console.log(e.foo); //baz console.log(e.message); //bazMessage console.log(e.stack); //stacktrace }
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.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.log(e.foo); //baz console.log(e.message) ;//bazMessage }
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.1. |
ECMAScript 5.1 (ECMA-262) The definition of 'Error' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Error' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'Error' in that specification. | Living Standard |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | 6 | Yes | Yes |
prototype |
Yes | Yes | Yes | 6 | Yes | Yes |
columnNumber |
No | No | Yes | No | No | No |
fileName |
No | No | Yes | No | No | No |
lineNumber |
No | No | Yes | No | No | No |
message |
Yes | Yes | Yes | 6 | Yes | Yes |
name |
Yes | Yes | Yes | 6 | Yes | Yes |
stack |
Yes | Yes | Yes | 10 | Yes | 6 |
toSource |
No | No | Yes | No | No | No |
toString |
Yes | Yes | Yes | 6 | Yes | Yes |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | Yes | 8.1 | Yes | Yes |
prototype |
Yes | Yes | Yes | Yes | 8.1 | Yes | Yes |
columnNumber |
No | No | No | Yes | No | No | No |
fileName |
No | No | No | Yes | No | No | No |
lineNumber |
No | No | No | Yes | No | No | No |
message |
Yes | Yes | Yes | Yes | 8.1 | Yes | Yes |
name |
Yes | Yes | Yes | Yes | 8.1 | Yes | Yes |
stack |
Yes | Yes | Yes | Yes | ? | Yes | 6 |
toSource |
No | No | No | Yes | No | No | No |
toString |
Yes | Yes | Yes | Yes | 8.1 | Yes | Yes |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error