JavaScript 异常处理
<h1>程序异常</h1><p> 程序异常可分为逻辑异常和语法异常,对于初学者而言语法异常居多,随着不断的学习对语言越来越熟悉后语法异常减少逻辑异常增多。</p>
<p> 在<code>JavaScript</code>中提供了对异常进行处理的语句,在适当的时候使用它们能够使程序变得更加健壮。</p>
<p> 但是要注意不要滥用异常处理,它会使程序的可读性变差。</p>
<h1>异常对象</h1>
<p> 所有的异常都是对象</p>
<h2>异常类型</h2>
<p> 在<code>JavaScript</code>中,常见异常类型有以下几种。</p>
<table>
<thead>
<tr><th>异常类型</th><th>简述</th></tr>
</thead>
<tbody>
<tr>
<td>Error</td>
<td><code>Error</code>是最基本的错误类型,其他的错误类型都继承自该类型。因此,<code>所有错误的类型共享了一组相同的属性。</code> 这个类型的错误很少见。一般使用开发人员自定义抛出的错误</td>
</tr>
<tr>
<td>EvalError</td>
<td>这个错误会在使用<code>eval()</code>函数发生异常时候抛出。两种情况会出错:1.使用<code>new</code>来进行实例化 2.尝试为变量起名为<code>eval</code>,因为它是一个关键字,所以这是不被允许的</td>
</tr>
<tr>
<td>RangeError</td>
<td>数值超出范围,常见于<code>Array</code>操作中</td>
</tr>
<tr>
<td>ReferenceError</td>
<td>变量找不到</td>
</tr>
<tr>
<td>SyntaxError</td>
<td>语法错误</td>
</tr>
<tr>
<td>TypeError</td>
<td>类型错误</td>
</tr>
<tr>
<td>URIError</td>
<td>在使用<code>encodeURI</code>或者<code>decodeURI</code>因为URL格式不正确时,就会导致URIError错误。</td>
</tr>
</tbody>
</table>
<h1>异常捕获</h1>
<h2>try catch</h2>
<p> 使用<code>try</code>与<code>catch</code>语句进行捕获异常。</p>
<blockquote>
<p> <code>try</code>用于检测可能出现异常的代码块</p>
<p><code> catch</code>用于处理捕捉到的异常,可指定参数获取异常信息</p>
</blockquote>
<p> <code>try...catch</code> 语句有一个包含一条或者多条语句的<code>try</code>代码块,0个或1个的<code>catch</code>代码块,<code>catch</code>代码块中的语句会在<code>try</code>代码块中抛出异常时执行。</p>
<p> 如果<code>try</code>代码块中的语句(或者<code>try</code> 代码块中调用的方法)一旦抛出了异常,那么执行流程会立即进入<code>catch</code> 代码块。</p>
<p> 如果<code>try</code>代码块没有抛出异常,<code>catch</code>代码块就会被跳过。</p>
<pre><code><script>
"use strict";
try {
console.log(username);
} catch (e) {// 会捕获异常的所有信息
console.log("处理了一个异常:", e);
}
/*
处理了一个异常: ReferenceError: username is not defined
at 1.html:56
*/
</script>
</code></pre>
<h2>finally</h2>
<p> <code>finally</code>块包含了在<code>try</code>和<code>catch</code>块完成后、下面接着<code>try...catch</code>的语句之前执行的语句。</p>
<p> <code>finally</code>块无论是否抛出异常都会执行。如果抛出了一个异常,就算没有异常处理,<code>finally</code>块里的语句也会执行。</p>
<pre><code><script>
"use strict";
try {
console.log(username);
} catch (e) {// 会捕获异常的所有信息
console.log("处理了一个异常,ID:", e);
} finally {
console.log("无论有没有异常都会执行我");
}
</script>
</code></pre>
<h1>主动异常</h1>
<h2>throw</h2>
<p> 在某些时候我们需要主动抛出异常,使用<code>throw</code>语句抛出异常。</p>
<pre><code><script>
"use strict";
throw new Error("这是一个错误")
</script>
</code></pre>
<h2>表达式形式</h2>
<p> 你可以抛出任意表达式而不是特定一种类型的表达式。下面的代码抛出了几个不同类型的表达式:</p>
<pre><code><script>
"use strict";
throw "Error2"; // String type
throw 42; // Number type
throw true; // Boolean type
throw { toString: function () { return "I'm an object!"; } };
</script>
</code></pre>
<h1>自定义异常</h1>
<p> 继承<code>Error</code>原型对象,可配置自定义的异常。</p>
<blockquote>
<p><code> Error</code>构造函数具有<code>message</code>可选参数,用于显示人类可阅读的错误描述信息</p>
</blockquote>
<h2>函数形式</h2>
<pre><code><script>
"use strict";
function MyError(message) {
this.name = 'MyError';
this.message = message || 'Default Message';
this.stack = (new Error()).stack;
}
Object.setPrototypeOf(MyError, Error);// 继承Error原型对象
try {
throw new MyError();
} catch (e) {
console.log(e.name);// MyError
console.log(e.message);// Default Message
}
try {
throw new MyError("自定义异常被抛出");
} catch (e) {
console.log(e.name);// MyError
console.log(e.message);// 自定义异常被抛出
}
</script>
</code></pre>
<h2>类形式</h2>
<pre><code><script>
"use strict";
class MyError extends Error {
constructor(message) {
super();
this.name = "MyError";
this.message = message || 'Default Message';
this.stack = (new Error()).stack;
}
}
try {
throw new MyError();
} catch (e) {
console.log(e.name);// MyError
console.log(e.message);// Default Message
}
try {
throw new MyError("自定义异常被抛出");
} catch (e) {
console.log(e.name);// MyError
console.log(e.message);// 自定义异常被抛出
}
</script>
</code></pre><br><br>
来源:https://www.cnblogs.com/Yunya-Cnblogs/p/13497203.html
頁:
[1]