--- title: Error slug: Web/JavaScript/Reference/Global_Objects/Error tags: - Error - JavaScript - NeedsTranslation - Reference - TopicStub translation_of: Web/JavaScript/Reference/Global_Objects/Error ---
Error
建構函式能用來建立一個 error 物件。當執行期間發生錯誤時,Error
物件實體會被拋出。Error
物件也可作為自訂例外的基礎物件,請參考下方的標準內建錯誤類型。
new Error([message[, fileName[, lineNumber]]])
message
{{optional_inline}}fileName
{{optional_inline}} {{non-standard_inline}}fileName
property on the created Error
object. Defaults to the name of the file containing the code that called the Error()
constructor.lineNumber
{{optional_inline}} {{non-standard_inline}}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 {{jsxref("Error.prototype")}}.
Besides the generic Error
constructor, there are six other core error constructors in JavaScript. For client-side exceptions, see Exception Handling Statements.
Error
instances.The global Error
object contains no methods of its own, however, it does inherit some methods through the prototype chain.
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) { console.log(e.name + ': ' + e.message); }
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) { 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. 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 |
---|---|---|
{{SpecName('ES1')}} | {{Spec2('ES1')}} | 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')}} | |
{{SpecName('ESDraft', '#sec-error-objects', 'Error')}} | {{Spec2('ESDraft')}} |
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}} |