We're looking for a person or people to help audit MDN to find places we could speed up. Is this you or someone you know? Check out the RFP: https://mzl.la/2IHcMiE

翻譯不完整。請協助 翻譯此英文文件

Error 建構函式能用來建立一個 error 物件。當執行期間發生錯誤時,Error 物件實體會被拋出。Error 物件也可作為自訂例外的基礎物件,請參考下方的標準內建錯誤類型。


new Error([message[, fileName[, lineNumber]]])


message 選擇性
fileName 選擇性
The value for the fileName property on the created Error object. Defaults to the name of the file containing the code that called the Error() constructor.
lineNumber 選擇性
Optional. The value for the 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 six other core error constructors in JavaScript. For client-side exceptions, see Exception Handling Statements.

Creates an instance representing an error that occurs regarding the global function eval().
Creates an instance representing an error that occurs when an internal error in the JavaScript engine is thrown. E.g. "too much recursion".
Creates an instance representing an error that occurs when a numeric variable or parameter is outside of its valid range.
Creates an instance representing an error that occurs when de-referencing an invalid reference.
Creates an instance representing a syntax error that occurs while parsing code in eval().
Creates an instance representing an error that occurs when a variable or parameter is not of a valid type.
Creates an instance representing an error that occurs when encodeURI() or decodeURI() are passed invalid parameters.


Allows the addition of properties to Error instances.


The global Error object contains no methods of its own, however, it does inherit some methods through the prototype chain.

Error 實體

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.


Standard properties

Specifies the function that created an instance's prototype.
Error message.
Error name.

Vendor-specific extensions

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 description. Similar to message.
Error number.


Path to file that raised this error.
Line number in file that raised this error.
Column number in line that raised this error.
Stack trace.


Returns a string containing the source of the specified Error object; you can use this value to create a new object. Overrides the Object.prototype.toSource() method.
Returns a string representing the specified object. Overrides the Object.prototype.toString() method.


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.log(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 {
} catch (e) {
  if (e instanceof EvalError) {
    console.log(e.name + ': ' + e.message);
  } else if (e instanceof RangeError) {
    console.log(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. 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 prototypically inherits from the Error constructor
function MyError(message) {
  this.name = 'MyError';
  this.message = message || 'Default Message';
  this.stack = (new Error()).stack;
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'


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.
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Error' in that specification.
ECMAScript Latest Draft (ECMA-262)
The definition of 'Error' in that specification.


We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)



 此頁面的貢獻者: jsgao0, jackblackevo, josephmcasey
 最近更新: jsgao0,