mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2026-03-08 15:02:05 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
<p></p><p></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>
|
||||
<pre><code class="language-javascript">new Error([<em>message</em>[, <em>fileName</em>[,<em>lineNumber</em>]]])</code></pre>
|
||||
<h3 id="Parameters" name="Parameters">参数</h3>
|
||||
<dl>
|
||||
<dt><code>message</code></dt>
|
||||
@@ -24,7 +24,7 @@
|
||||
<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>
|
||||
const y = new Error('I was constructed via the "new" keyword!');</code></code></pre>
|
||||
<p> </p>
|
||||
<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>
|
||||
@@ -108,15 +108,15 @@ const y = new Error('I was constructed via the "new" keyword!');</code></pre>
|
||||
<h2 id="Examples" name="Examples">例子</h2>
|
||||
<h3 id="Example:_Throwing_a_generic_error" name="Example:_Throwing_a_generic_error">抛出一个基本错误</h3>
|
||||
<p>通常你会使用<a href="Reference/Statements/throw" title="throw语句用来抛出一个用户自定义的异常。当前函数的执行将被停止(throw之后的语句将不会执行),并且控制将被传递到调用堆栈中的第一个catch块。如果调用者函数中没有catch块,程序将会终止。"><code>throw</code></a>关键字来抛出你创建的Error对象。可以使用 <a href="Reference/Statements/try...catch" title="try...catch语句将能引发错误的代码放在try块中,并且对应一个响应,然后有异常被抛出。"><code>try...catch</code></a> 结构来处理异常:</p>
|
||||
<pre class="brush: js">try {
|
||||
<pre><code class="language-javascript">try {
|
||||
throw new Error("Whoops!");
|
||||
} catch (e) {
|
||||
alert(e.name + ": " + e.message);
|
||||
}
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="Example:_Handling_a_specific_error" name="Example:_Handling_a_specific_error">处理一个特定错误</h3>
|
||||
<p>你可以通过判断异常的类型来特定处理某一类的异常,即判断 <a href="Reference/Global_Objects/Object/constructor" title='返回创建实例对象的 Object 构造函数的引用。注意,此属性的值是对函数本身的引用,而不是一个包含函数名称的字符串。对原始类型来说,如1,true和"test",该值只可读。'><code>constructor</code></a> 属性,当使用现代Javascript引擎时,可使用<a href="Reference/Operators/instanceof" title="instanceof运算符用于测试构造函数的prototype属性是否出现在对象的原型链中的任何位置"><code>instanceof</code></a> 关键字:</p>
|
||||
<pre class="brush: js">try {
|
||||
<pre><code class="language-javascript">try {
|
||||
foo.bar();
|
||||
} catch (e) {
|
||||
if (e instanceof EvalError) {
|
||||
@@ -126,14 +126,14 @@ const y = new Error('I was constructed via the "new" keyword!');</code></pre>
|
||||
}
|
||||
// ... etc
|
||||
}
|
||||
</pre>
|
||||
</code></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 class="external" href="http://stackoverflow.com/questions/1382107/whats-a-good-way-to-extend-error-in-javascript" rel="noopener">"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.
|
||||
<pre><code class="language-javascript">// Create a new object, that prototypally inherits from the Error constructor.
|
||||
function MyError(message) {
|
||||
this.name = 'MyError';
|
||||
this.message = message || 'Default Message';
|
||||
@@ -154,7 +154,7 @@ try {
|
||||
} catch (e) {
|
||||
console.log(e.name); // 'MyError'
|
||||
console.log(e.message); // 'custom message'
|
||||
}</pre>
|
||||
}</code></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>
|
||||
|
||||
Reference in New Issue
Block a user