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

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

@@ -6,21 +6,21 @@
<p>The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>
</div>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">throw <em>expression</em>; </pre>
<pre><code class="language-javascript">throw <em>expression</em>; </code></pre>
<dl>
<dt><code>expression</code></dt>
<dd>要抛出的表达式。</dd>
</dl>
<h2 id="描述">描述</h2>
<p>使用<code>throw</code>语句来抛出一个异常。当你抛出异常时,<code>expression</code> 指定了异常的内容。下面的每行都抛出了一个异常:</p>
<pre class="brush: js">throw "Error2"; // 抛出了一个值为字符串的异常
<pre><code class="language-javascript">throw "Error2"; // 抛出了一个值为字符串的异常
throw 42; // 抛出了一个值为整数42的异常
throw true; // 抛出了一个值为true的异常</pre>
throw true; // 抛出了一个值为true的异常</code></pre>
<p>注意<code>throw</code>语句同样受到<a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion">自动分号插入ASI</a>)机制的控制,在<code>throw</code>关键字和值之间不允许有行终止符。</p>
<h2 id="示例">示例</h2>
<h3 id="抛出一个对象">抛出一个对象</h3>
<p>你可以在抛出异常时指定一个对象。然后可以在<code>catch</code>块中引用对象的属性。以下示例创建一个类型为<code>UserException</code>的对象,并在<code>throw</code>语句中使用它。</p>
<pre class="brush: js">function UserException(message) {
<pre><code class="language-javascript">function UserException(message) {
this.message = message;
this.name = "UserException";
}
@@ -43,10 +43,10 @@ try {
var monthName = "unknown";
console.log(e.message, e.name); // 传递异常对象到错误处理
}
</pre>
</code></pre>
<h3 id="另一个抛出异常对象的示例">另一个抛出异常对象的示例</h3>
<p>下面的示例中测试一个字符串是否是美国邮政编码。如果邮政编码是无效的,那么<code>throw</code>语句将会抛出一个类型为 <code>ZipCodeFormatException</code>的异常对象实例。</p>
<pre class="brush: js">/*
<pre><code class="language-javascript">/*
* 创建 ZipCode 示例.
*
* 可被接受的邮政编码格式:
@@ -108,10 +108,10 @@ b = verifyZipCode(9560); // 返回 -1
c = verifyZipCode("a"); // 返回 -1
d = verifyZipCode("95060"); // 返回 95060
e = verifyZipCode("95060 1234"); // 返回 95060 1234
</pre>
</code></pre>
<h3 id="重新抛出异常">重新抛出异常</h3>
<p>你可以使用<code>throw</code>来抛出异常。下面的例子捕捉了一个异常值为数字的异常并在其值大于50后重新抛出异常。重新抛出的异常传播到闭包函数或顶层以便用户看到它。</p>
<pre class="brush: js">try {
<pre><code class="language-javascript">try {
throw n; // 抛出一个数值异常
} catch (e) {
if (e &lt;= 50) {
@@ -121,7 +121,7 @@ e = verifyZipCode("95060 1234"); // 返回 95060 1234
throw e;
}
}
</pre>
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>