语法高亮,滚动条美化,设置页面调整

This commit is contained in:
fofolee
2019-04-19 02:41:09 +08:00
parent 1e8f76c000
commit 359d29ee0b
1590 changed files with 12328 additions and 11441 deletions

View File

@@ -2,7 +2,7 @@
<div></div>
<p> <strong><code>try...catch</code></strong>语句将能引发错误的代码放在try块中并且对应一个响应然后有异常被抛出。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">try {
<pre><code class="language-javascript">try {
<em>try_statements</em>
}
[catch (<em>exception_var_1</em> if <em>condition_1</em>) { // non-standard
@@ -15,7 +15,7 @@
[finally {
<em>finally_statements</em>
}]
</pre>
</code></pre>
<dl>
<dt><code>try_statements</code></dt>
<dd>需要被执行的语句。</dd>
@@ -49,14 +49,14 @@
<p>你也可以用<code>try</code>语句去处理 JavaScript 异常。参考<a href="Guide">JavaScript 指南</a>了解更多关于 Javascript 异常的信息。</p>
<h3 id="无条件的catch子句">无条件的<code>catch</code>子句</h3>
<p>当使用单个无条件<code>catch</code>子句时,抛出的任何异常时都会进入到<code>catch</code>块。例如,当在下面的代码中发生异常时,控制转移到<code>catch</code>子句。</p>
<pre class="brush: js">try {
<pre><code class="language-javascript">try {
throw "myException"; // generates an exception
}
catch (e) {
// statements to handle any exceptions
logMyErrors(e); // pass exception object to error handler
}
</pre>
</code></pre>
<p><code>catch</code>块指定一个标识符在上面的示例中为e该标识符保存由<code>throw</code>语句指定的值。<code>catch</code>块是唯一的,因为当输入<code>catch</code>块时JavaScript 会创建此标识符,并将其添加到当前作用域;标识符仅在<code>catch</code>块执行时存在;<code>catch</code>块执行完成后,标识符不再可用。</p>
<h3 id="条件catch子句">条件<code>catch</code>子句</h3>
<p></p><div class="blockIndicator nonStandard nonStandardHeader">
@@ -66,7 +66,7 @@ catch (e) {
<p>你也可以用一个或者更多条件<code>catch</code>子句来处理特定的异常。在这种情况下,当异常抛出时将会进入合适的<code>catch</code>子句中。在下面的代码中,<code>try</code>块的代码可能会抛出三种异常:<a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a><a href="Reference/Global_Objects/RangeError" title="RangeError对象标明一个错误当一个值不在其所允许的范围或者集合中。"><code>RangeError</code></a><a href="Reference/Global_Objects/EvalError" title="本对象代表了一个关于 eval 函数的错误.此异常不再会被JavaScript抛出但是EvalError对象仍然保持兼容性."><code>EvalError</code></a>。当一个异常抛出时,控制将会进入与其对应的<code>catch</code>语句。如果这个异常不是特定的,那么控制将转移到无条件<code>catch</code>子句。</p>
<p>当用一个无条件<code>catch</code>子句和一个或多个条件语句时,无条件<code>catch</code>子句必须放在最后。否则当到达条件语句之前所有的异常将会被非条件语句拦截。</p>
<p>提醒:这个功能不符合 ECMAscript 规范。</p>
<pre class="brush: js">try {
<pre><code class="language-javascript">try {
myroutine(); // may throw three types of exceptions
} catch (e if e instanceof TypeError) {
// statements to handle TypeError exceptions
@@ -78,9 +78,9 @@ catch (e) {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
</pre>
</code></pre>
<p>下面用符合 ECMAscript 规范的简单的 JavaScript 来编写相同的“条件catch子句”显然更加<span class="s1">冗长的</span>,但是可以在任何地方运行):</p>
<pre class="brush: js">try {
<pre><code class="language-javascript">try {
myroutine(); // may throw three types of exceptions
} catch (e) {
if (e instanceof TypeError) {
@@ -94,7 +94,7 @@ catch (e) {
logMyErrors(e); // pass exception object to error handler
}
}
</pre>
</code></pre>
<h3 id="异常标识符">异常标识符</h3>
<p><code>try</code>块中的抛出一个异常时, <em><code>exception_var</code></em>(如<code>catch (e)</code>中的<code>e</code>)用来保存被抛出声明指定的值。你可以用这个标识符来获取关于被抛出异常的信息。</p>
<p>这个标识符是<code>catch</code>子语句内部的。换言之,当进入<code>catch</code>子语句时标识符创建,<code>catch</code>子语句执行完毕后,这个标识符将不再可用。</p>
@@ -106,7 +106,7 @@ catch (e) {
<p>因此,外部的包裹<code>try-block</code>(或主流)退出之前完成常规清理的代码将被跳过。然而,如果<code>try-block</code>有一个<code>finally-block</code>,那么<code>finally-block</code>代码将可以先来执行这些清理然后另一个try的<code>catch-block</code>(或者错误发生器)再来处理第二个异常。</p>
<p>现在,如果无论<code>try..catch</code>代码执行成功与否,清理例程都必须执行,并且<code>finally-block</code>只在发生异常时才执行,那么<code>finally-block</code>内部和外部需要多复制一份同样的清理例程,所以没有理由不只保留一个<code>finally-block</code>,并且不管是否有异常,都让它执行。</p>
<p>以下示例打开一个文件,然后执行使用该文件的语句(服务器端 JavaScript 允许您访问文件)。如果文件打开时抛出异常,则<code>finally</code>子句会在脚本失败之前关闭该文件。finally中的代码最终也会在<code>try</code><code>catch block</code>显式返回时执行。</p>
<pre class="brush: js">openMyFile()
<pre><code class="language-javascript">openMyFile()
try {
// tie up a resource
writeMyFile(theData);
@@ -114,11 +114,11 @@ try {
finally {
closeMyFile(); // always close the resource
}
</pre>
</code></pre>
<h2 id="示例">示例</h2>
<h3 id="嵌套_try_块">嵌套 try 块</h3>
<p>首先让我们看看这里发生什么:</p>
<pre class="brush: js">try {
<pre><code class="language-javascript">try {
try {
throw new Error("oops");
}
@@ -133,9 +133,9 @@ catch (ex) {
// Output:
// "finally"
// "outer" "oops"
</pre>
</code></pre>
<p>现在,如果我们已经捕获异常通过增加一个 catch 语句块</p>
<pre class="brush: js">try {
<pre><code class="language-javascript">try {
try {
throw new Error("oops");
}
@@ -153,9 +153,9 @@ catch (ex) {
// Output:
// "inner" "oops"
// "finally"
</pre>
</code></pre>
<p>现在,让我们再次抛出错误。</p>
<pre class="brush: js">try {
<pre><code class="language-javascript">try {
try {
throw new Error("oops");
}
@@ -175,11 +175,11 @@ catch (ex) {
// "inner" "oops"
// "finally"
// "outer" "oops"
</pre>
</code></pre>
<p>任何给定的异常只会被离它最近的封闭 catch 块捕获一次。当然在“inner”块抛出的任何新异常 因为 catch 块里的代码也可以抛出异常将会被“outer”块所捕获。</p>
<h3 id="从_finally_语句块返回"> finally 语句块返回</h3>
<p>如果从<code>finally</code>块中返回一个值,那么这个值将会成为整个<code>try-catch-finally</code>的返回值,无论是否有<code>return</code>语句在<code>try</code><code>catch</code>中。这包括在<code>catch</code>块里抛出的异常。</p>
<pre class="brush: js">try {
<pre><code class="language-javascript">try {
try {
throw new Error("oops");
}
@@ -200,7 +200,7 @@ catch (ex) {
// Output:
// "inner" "oops"
// "finally"
</pre>
</code></pre>
<p>因为 finally 块里的 return 语句,"oops" 没有抛出到外层, 从 catch 块返回的值同样适用。</p>
<h2 id="规范">规范</h2>
<table class="standard-table">