Summary
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 objects for user-defined exceptions. See below for standard built-in error types.
Syntax
new Error([message[, fileName[, lineNumber]]])
Parameters
message
- Optional. Human-readable description of the error.
fileName
{{non-standard_inline}}- Optional. The value for the
fileName
property on the createdError
object. Defaults to the name of the file containing the code that called theError()
constructor. lineNumber
{{non-standard_inline}}- Optional. The value for the
lineNumber
property on the createdError
object. 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 {{jsxref("Error.prototype")}}.
Error types
Besides the generic Error
constructor, there are six other core error constructors in JavaScript. For client-side exceptions, see Exception Handling Statements.
- {{jsxref("Global_Objects/EvalError", "EvalError")}}
- Creates an instance representing an error that occurs regarding the global function {{jsxref("Global_Objects/eval", "eval()")}}.
- {{jsxref("Global_Objects/InternalError", "InternalError")}} {{non-standard_inline}}
- Creates an instance representing an error that occurs when an internal error in the JavaScript engine is thrown. E.g. "too much recursion".
- {{jsxref("Global_Objects/RangeError", "RangeError")}}
- Creates an instance representing an error that occurs when a numeric variable or parameter is outside of its valid range.
- {{jsxref("Global_Objects/ReferenceError", "ReferenceError")}}
- Creates an instance representing an error that occurs when de-referencing an invalid reference.
- {{jsxref("Global_Objects/SyntaxError", "SyntaxError")}}
- Creates an instance representing a syntax error that occurs while parsing code in {{jsxref("Global_Objects/eval", "eval()")}}.
- {{jsxref("Global_Objects/TypeError", "TypeError")}}
- Creates an instance representing an error that occurs when a variable or parameter is not of a valid type.
- {{jsxref("Global_Objects/URIError", "URIError")}}
- Creates an instance representing an error that occurs when {{jsxref("Global_Objects/encodeURI", "encodeURI()")}} or {{jsxref("Global_Objects/decodeURI", "decodeURI()")}} are passed invalid parameters.
Properties
- {{jsxref("Error.prototype")}}
- Allows the addition of properties to
Error
instances.
Methods
The global Error
object contains no methods of its own, however, it does inherit some methods through the prototype chain.
Error
instances
Properties
Methods
Examples
Example: Throwing a generic error
Usually you create an Error
object with the intention of raising it using the {{jsxref("Statements/throw", "throw")}} keyword. You can handle the error using the {{jsxref("Statements/try...catch", "try...catch")}} construct:
try { throw new Error('Whoops!'); } catch (e) { alert(e.name + ': ' + e.message); }
Example: Handling a specific error
this should probably be removed You can choose to handle only specific error types by testing the error type with the error's {{jsxref("Object.prototype.constructor", "constructor")}} property or, if you're writing for modern JavaScript engines, {{jsxref("Operators/instanceof", "instanceof")}} keyword:
try { foo.bar(); } catch (e) { if (e instanceof EvalError) { alert(e.name + ': ' + e.message); } else if (e instanceof RangeError) { alert(e.name + ': ' + e.message); } // ... etc }
Example: 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. The common way to do this is demonstrated below.
Note that the thrown MyError
will report incorrect lineNumber
and fileName
at least in Firefox.
See also the "What's a good way to extend Error in JavaScript?" discussion on Stackoverflow.
// Create a new object, that prototypally inherits from the Error constructor. function MyError(message) { this.name = 'MyError'; this.message = message || 'Default Message'; } MyError.prototype = Object.create(Error.prototype); MyError.prototype.constructor = MyError; try { throw new MyError(); } catch (e) { console.log(e.name); // 'MyError' console.log(e.message); // 'Default Message' } try { throw new MyError('custom message'); } catch (e) { console.log(e.name); // 'MyError' console.log(e.message); // 'custom message' }
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition. | Standard | Initial definition. Implemented in JavaScript 1.1. |
{{SpecName('ES5.1', '#sec-15.11', 'Error')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-error-objects', 'Error')}} | {{Spec2('ES6')}} |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
See also
- {{jsxref("Error.prototype")}}
- {{jsxref("Statements/throw", "throw")}}
- {{jsxref("Statements/try...catch", "try...catch")}}