--- title: Error slug: Web/JavaScript/Reference/Global_Objects/Error tags: - Error - JavaScript - NeedsTranslation - Reference - TopicStub translation_of: Web/JavaScript/Reference/Global_Objects/Error --- <div>{{JSRef}}</div> <p><strong><code>Error</code></strong> 建構函式能用來建立一個 error 物件。當執行期間發生錯誤時,<code>Error</code> 物件實體會被拋出。<code>Error</code> 物件也可作為自訂例外的基礎物件,請參考下方的標準內建錯誤類型。</p> <h2 id="語法">語法</h2> <pre class="syntaxbox">new Error([<var>message</var>[, <var>fileName</var>[, <var>lineNumber</var>]]])</pre> <h3 id="參數">參數</h3> <dl> <dt><code>message</code> {{optional_inline}}</dt> <dd>人們可閱讀的錯誤說明。</dd> <dt><code>fileName</code> {{optional_inline}} {{non-standard_inline}}</dt> <dd>The value for the <code>fileName</code> property on the created <code>Error</code> object. Defaults to the name of the file containing the code that called the <code>Error()</code> constructor.</dd> <dt><code>lineNumber</code> {{optional_inline}} {{non-standard_inline}}</dt> <dd>Optional. The value for the <code>lineNumber</code> property on the created <code>Error</code> object. Defaults to the line number containing the <code>Error()</code> constructor invocation.</dd> </dl> <h2 id="描述">描述</h2> <p>Runtime errors result in new <code>Error</code> objects being created and thrown.</p> <p>This page documents the use of the <code>Error</code> object itself and its use as a constructor function. For a list of properties and methods inherited by <code>Error</code> instances, see {{jsxref("Error.prototype")}}.</p> <h3 id="錯誤類型">錯誤類型</h3> <p>Besides the generic <code>Error</code> constructor, there are six other core error constructors in JavaScript. For client-side exceptions, see <a href="/en-US/docs/Web/JavaScript/Guide/Statements#Exception_Handling_Statements">Exception Handling Statements</a>.</p> <dl> <dt>{{jsxref("EvalError")}}</dt> <dd>Creates an instance representing an error that occurs regarding the global function {{jsxref("Global_Objects/eval", "eval()")}}.</dd> <dt>{{jsxref("InternalError")}} {{non-standard_inline}}</dt> <dd>Creates an instance representing an error that occurs when an internal error in the JavaScript engine is thrown. E.g. "too much recursion".</dd> <dt>{{jsxref("RangeError")}}</dt> <dd>Creates an instance representing an error that occurs when a numeric variable or parameter is outside of its valid range.</dd> <dt>{{jsxref("ReferenceError")}}</dt> <dd>Creates an instance representing an error that occurs when de-referencing an invalid reference.</dd> <dt>{{jsxref("SyntaxError")}}</dt> <dd>Creates an instance representing a syntax error that occurs while parsing code in {{jsxref("Global_Objects/eval", "eval()")}}.</dd> <dt>{{jsxref("TypeError")}}</dt> <dd>Creates an instance representing an error that occurs when a variable or parameter is not of a valid type.</dd> <dt>{{jsxref("URIError")}}</dt> <dd>Creates an instance representing an error that occurs when {{jsxref("Global_Objects/encodeURI", "encodeURI()")}} or {{jsxref("Global_Objects/decodeURI", "decodeURI()")}} are passed invalid parameters.</dd> </dl> <h2 id="屬性">屬性</h2> <dl> <dt>{{jsxref("Error.prototype")}}</dt> <dd>Allows the addition of properties to <code>Error</code> instances.</dd> </dl> <h2 id="方法">方法</h2> <p>The global <code>Error</code> object contains no methods of its own, however, it does inherit some methods through the prototype chain.</p> <h2 id="Error_實體"><code>Error</code> 實體</h2> <div>{{page('en-US/docs/JavaScript/Reference/Global_Objects/Error/prototype', 'Description')}}</div> <h3 id="屬性_2">屬性</h3> <div>{{page('en-US/docs/JavaScript/Reference/Global_Objects/Error/prototype', 'Properties')}}</div> <h3 id="方法_2">方法</h3> <div>{{page('en-US/docs/JavaScript/Reference/Global_Objects/Error/prototype', 'Methods')}}</div> <h2 id="範例">範例</h2> <h3 id="Throwing_a_generic_error">Throwing a generic error</h3> <p>Usually you create an <code>Error</code> 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:</p> <pre class="brush: js">try { throw new Error('Whoops!'); } catch (e) { console.log(e.name + ': ' + e.message); } </pre> <h3 id="Handling_a_specific_error">Handling a specific error</h3> <p>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:</p> <pre class="brush: js">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 } </pre> <h3 id="Custom_Error_Types">Custom Error Types</h3> <p>You might want to define your own error types deriving from <code>Error</code> to be able to <code>throw new MyError()</code> and use <code>instanceof MyError</code> to check the kind of error in the exception handler. The common way to do this is demonstrated below.</p> <div class="notecard warning"> <p><strong>Warning:</strong> Note that the thrown <code>MyError</code> will report incorrect <code>lineNumber</code> and <code>fileName</code> at least in Firefox.</p> </div> <p>See also the <a href="http://stackoverflow.com/questions/1382107/whats-a-good-way-to-extend-error-in-javascript">"What's a good way to extend Error in JavaScript?" discussion on Stackoverflow</a>.</p> <pre class="brush: js">// 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' }</pre> <h2 id="規範">規範</h2> {{Specifications}} <h2 id="瀏覽器相容性">瀏覽器相容性</h2> {{Compat}} <h2 id="參見">參見</h2> <ul> <li>{{jsxref("Error.prototype")}}</li> <li>{{jsxref("Statements/throw", "throw")}}</li> <li>{{jsxref("Statements/try...catch", "try...catch")}}</li> </ul>