aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/error/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/error/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/error/index.html257
1 files changed, 257 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/error/index.html b/files/zh-cn/web/javascript/reference/global_objects/error/index.html
new file mode 100644
index 0000000000..27bd521d4e
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/error/index.html
@@ -0,0 +1,257 @@
+---
+title: Error
+slug: Web/JavaScript/Reference/Global_Objects/Error
+tags:
+ - Error
+ - Whoops!
+ - 参考
+translation_of: Web/JavaScript/Reference/Global_Objects/Error
+---
+<p>{{JSRef}}</p>
+
+<p><span style="line-height: inherit;">通过<strong>Error</strong>的构造器可以创建一个错误对象。当运行时错误产生时,Error的实例对象会被抛出。Error对象</span>也<span style="line-height: inherit;">可用于用户自定义的异常的基础对象。下面列出了各种内建的标准错误类型。</span></p>
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="brush: js">new Error([<em>message</em>[, <em>fileName</em>[,<em>lineNumber</em>]]])</pre>
+
+<h3 id="Parameters" name="Parameters">参数</h3>
+
+<dl>
+ <dt><code>message</code></dt>
+ <dd>可选。人类可阅读的错误描述信息。</dd>
+</dl>
+
+<dl>
+ <dt><code>fileName </code>{{non-standard_inline}}</dt>
+ <dd>可选。被创建的<span style="font-family: monospace; line-height: inherit;">Error对象的fileName属性值。</span><span style="line-height: inherit;">默认是调用Error构造器代码所在的文件 的名字。</span></dd>
+</dl>
+
+<dl>
+ <dt><code>lineNumber </code>{{non-standard_inline}}</dt>
+ <dd>可选。被创建的<span style="font-family: monospace; line-height: inherit;">Error对象的lineNumber属性值。默认是</span><span style="line-height: inherit;">调用Error构造器代码所在的文件的行号</span><span style="font-family: monospace; line-height: inherit;">。</span></dd>
+</dl>
+
+<h2 id="Description" name="Description">描述</h2>
+
+<p>当代码运行时的发生错误,会创建新的<code>Error</code> 对象,并将其抛出。</p>
+
+<p><span style="line-height: inherit;">该页面描述了Error对象自身的使用,以及其构造函数的使用</span><span style="line-height: inherit;">. 关于Error实例的内部属性和方法,请看</span> {{jsxref("Error.prototype")}}。</p>
+
+<h3 id="作为函数使用">作为函数使用</h3>
+
+<p>当像函数一样使用 <code>Error</code> 时 -- 如果没有 {{jsxref("Operators/new", "new")}},它将返回一个 <code>Error</code> 对象。所以, 仅仅调用 <code>Error</code> 产生的结果与通过<code>new</code> 关键字构造 <code>Error</code> 对象生成的结果相同。 </p>
+
+<pre><code>// this:
+const x = Error('I was created using a function call!');
+​​​​// has the same functionality as this:
+const y = new Error('I was constructed via the "new" keyword!');</code></pre>
+
+<h3 id="Error_types" name="Error_types">Error 类型</h3>
+
+<p>除了通用的<span style="font-family: courier new,andale mono,monospace; line-height: inherit;">Error构造函数外,</span>JavaScript<span style="font-family: courier new,andale mono,monospace; line-height: inherit;">还有6个其他类型的错误构造函数。更多客户端异常,详见</span><span style="line-height: inherit;"> </span><a href="/en/JavaScript/Guide/Statements#Exception_Handling_Statements" style="line-height: inherit;" title="en/JavaScript/Guide/Statements#Exception Handling Statements">Exception Handling Statements</a>。</p>
+
+<dl>
+ <dt><strong style="font-weight: bold;">{{jsxref("EvalError")}}</strong></dt>
+ <dd>创建一个error实例,表示错误的原因:与 {{jsxref("Global_Objects/eval", "eval()")}} 有关。</dd>
+ <dt><strong style="font-weight: bold;">{{jsxref("InternalError")}} {{non-standard_inline}}</strong></dt>
+ <dd>创建一个代表Javascript引擎内部错误的异常抛出的实例。 如: "递归太多".</dd>
+</dl>
+
+<dl>
+ <dt><strong style="font-weight: bold;">{{jsxref("RangeError", "RangeError")}}</strong></dt>
+ <dd>创建一个error实例,表示错误的原因:<span style="line-height: inherit;">数值变量或参数超出其有效范围</span><span style="line-height: inherit;">。</span></dd>
+</dl>
+
+<dl>
+ <dt><strong style="font-weight: bold;">{{jsxref("ReferenceError")}}</strong></dt>
+ <dd>创建一个error实例,表示错误的原因:无效引用。</dd>
+</dl>
+
+<dl>
+ <dt><strong style="font-weight: bold;">{{jsxref("SyntaxError")}}</strong></dt>
+ <dd>创建一个error实例,表示错误的原因:{{jsxref("Global_Objects/eval", "eval()")}}在解析代码的过程中发生的语法错误<span style="line-height: inherit;">。</span></dd>
+</dl>
+
+<dl>
+ <dt><strong style="font-weight: bold;">{{jsxref("TypeError")}}</strong></dt>
+ <dd>创建一个error实例,表示错误的原因:变量或参数不属于有效类型。</dd>
+</dl>
+
+<dl>
+ <dt><strong style="font-weight: bold;">{{jsxref("URIError")}}</strong></dt>
+ <dd>创建一个error实例,表示错误的原因:给 {{jsxref("Global_Objects/encodeURI", "encodeURI()")}}<span style="line-height: inherit;">或 </span> {{jsxref("Global_Objects/decodeURI", "decodeURl()")}}<span style="line-height: inherit;">传递的参数无效。</span></dd>
+</dl>
+
+<h2 id="Properties" name="Properties">属性</h2>
+
+<dl>
+ <dt>{{jsxref("Error.prototype")}}</dt>
+ <dd>允许添加属性到<code>Error</code>实例。</dd>
+</dl>
+
+<h2 id="Methods" name="Methods">方法</h2>
+
+<p>全局<code>Error</code>对象自身不包含任何方法,但从原型链中继承了一些方法.</p>
+
+<h2 id="Error_instances" name="Error_instances"><code>Error</code> 实例</h2>
+
+<div>
+<p>{{page('en-US/docs/JavaScript/Reference/Global_Objects/Error/prototype', 'Description')}}</p>
+
+<h3 id="属性">属性</h3>
+
+<p>{{page('en-US/docs/JavaScript/Reference/Global_Objects/Error/prototype', 'Properties')}}</p>
+
+<h3 id="方法">方法</h3>
+
+<p>{{page('en-US/docs/JavaScript/Reference/Global_Objects/Error/prototype', 'Methods')}}</p>
+</div>
+
+<h2 id="Examples" name="Examples">例子</h2>
+
+<h3 id="Example_Throwing_a_generic_error" name="Example:_Throwing_a_generic_error">抛出一个基本错误</h3>
+
+<p>通常你会使用{{jsxref("Statements/throw", "throw")}}关键字来抛出你创建的Error对象。可以使用 {{jsxref("Statements/try...catch", "try...catch")}} 结构来处理异常:</p>
+
+<pre class="brush: js">try {
+ throw new Error("Whoops!");
+} catch (e) {
+ alert(e.name + ": " + e.message);
+}
+</pre>
+
+<h3 id="Example_Handling_a_specific_error" name="Example:_Handling_a_specific_error">处理一个特定错误</h3>
+
+<p>你可以通过判断异常的类型来特定处理某一类的异常,即判断 {{jsxref("Object.prototype.constructor", "constructor")}} 属性,当使用现代Javascript引擎时,可使用{{jsxref("Operators/instanceof", "instanceof")}} 关键字:</p>
+
+<pre class="brush: js">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
+}
+</pre>
+
+<h3 id="自定义异常类型">自定义异常类型</h3>
+
+<p>你可能希望自定义基于Error的异常类型,使得你能够 throw new MyError() 并可以使用 <code>instanceof MyError</code> 来检查某个异常的类型. 这种需求的通用解决方法如下.</p>
+
+<div class="warning" style="font-size: 14px;">
+<p>注意,在FireFox中抛出自定义类型的异常会显示不正确的行号和文件名。</p>
+</div>
+
+<p>参考 <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 prototypally 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>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 0px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 19px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 38px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 57px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 76px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 95px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 114px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 133px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 152px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 171px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 190px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 209px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 228px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 247px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 266px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 285px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 304px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 323px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 342px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 361px; background: 0px 0px;"></div>
+
+<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 380px; background: 0px 0px;"></div>
+
+<h2 id="规范" style="margin-bottom: 20px; line-height: 30px; font-size: 2.14285714285714rem;">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>ECMAScript 1st Edition.</td>
+ <td>Standard</td>
+ <td>Initial definition. Implemented in JavaScript 1.1.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-15.11', 'Error')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-error-objects', 'Error')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-error-objects', 'Error')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性" style="margin-bottom: 20px; line-height: 30px; font-size: 2.14285714285714rem;">浏览器兼容性</h2>
+
+<div>{{Compat("javascript.builtins.Error")}}</div>
+
+<h2 id="See_also" name="See_also" style="margin-bottom: 20px; line-height: 30px; font-size: 2.14285714285714rem;">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("Error.prototype")}}</li>
+ <li>{{jsxref("Statements/throw", "throw")}}</li>
+ <li>{{jsxref("Statements/try...catch", "try...catch")}}</li>
+</ul>