aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/error/index.html
blob: 27bd521d4e3efa410ee46d6e6c34fe11fbe102b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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>