如何在 JavaScript 中使用 throw 语句?
在本教程中,我们将学习如何在 JavaScript 中使用 throw 语句。“throw”是 JavaScript 中的保留关键字,程序员可以使用 throw 关键字创建**用户自定义异常**。
每个程序员都不是完美的,所以他们不可能编写 JavaScript 代码而不出任何错误。程序员也可能会从用户那里获取输入,如果用户输入无效,则会发生异常。一些内置异常包括算术异常、索引越界异常等。在某些情况下,程序员希望创建自己的异常,并且可以使用 try…catch 块中的 throw 语句来实现。
下面,我们给出了一些在 JavaScript 中使用 throw 关键字的不同示例。
使用 throw 关键字创建异常
在本节中,我们将学习如何使用**throw**语句创建简单的用户定义异常。当发生任何错误时,我们将返回简单的字符串消息。当我们抛出错误时,程序执行控制将转到最近的 catch 块以处理异常。如果我们没有定义任何 catch 块,则程序执行将因异常错误而终止。
语法
用户可以按照以下语法使用 throw 语句。
try {
// program code
If ( condition ){
throw 'error message';
}
} catch (error) {
// handle user-defined exception.
}示例
在下面的示例中,当我们添加输入数字并且 10 大于 25 时,我们将抛出错误。我们只抛出一个错误消息。
<html>
<head>
<title>Example - Use of the throw keyword in JavaScript. </title>
</head>
<body>
<h2>Use of the throw keyword in JavaScript. </h2>
<h4> Program execution flow, when we use the throw keyword to throw an user defined exception. </h4>
<div id="value"></div>
<script>
let value = document.getElementById("value");
try {
value.innerHTML += "control is inside the try block. </br>";
let number = 20;
if (number + 10 > 25) {
// throw error.
throw "number is large, it is not acceptable.";
value.innerHTML += "inside if block. </br>";
}
} catch (error) {
// handling the exception.
value.innerHTML += "inside the catch block. <br/>";
value.innerHTML += "error message is: " + error + " <br/>";
}
</script>
</body>
</html>在上面的输出中,用户可以看到由于抛出错误,try 块的执行将不会完成。控制权转到 catch 块并处理错误。
使用 throw 语句创建新的 Error 对象
在上一节中,我们创建了简单的错误消息来抛出异常,但在这节中,我们将创建一个新的**用户定义的 Error 对象**。JavaScript 有一个名为 Error 的内置类来管理异常。我们也可以创建 Error 类的对象实例并将错误消息作为参数传递。
语法
用户可以查看以下语法来抛出新的 Error 对象。
try {
// program code
if ( condition ){
throw new Error("message");
}
} catch (error) {
// handle user-defined exception.
}示例
在下面的示例中,我们正在检查变量的类型。如果变量类型未定义,我们将抛出一个用户定义的 Error 对象以及错误消息。因此,我们可以在 catch 块中处理异常。
<html>
<head>
<title>Example - Use of the throw keyword in JavaScript. </title>
</head>
<body>
<h2> Use of the throw statement in JavaScript. </h2>
<h4> Program execution flow, when we use the throw keyword to throw an user defined exception. </h4>
<div id="value"></div>
<script>
let value = document.getElementById("value");
try {
value.innerHTML += "control is inside the try block. </br>";
if (typeof number == "undefined") {
// throw error.
throw new Error("variable is undefined.");
}
value.innerHTML += "terminating the try block. <br/>";
} catch (error) {
value.innerHTML += "inside the catch block. <br/>";
value.innerHTML += error.name + ": " + error.message + " <br/>";
}
</script>
</body>
</html>使用 throw 语句重新抛出异常
在本节中,我们将不在 catch 块中处理错误,而是将错误从 catch 块中再次抛出。
语法
try {
If ( condition ){
throw new Error("message");
}
} catch (error) {
throw "unknown error."
}示例
在下面的示例中,我们根据条件语句抛出两个不同的错误。我们在 catch 块中处理一个错误,并从 catch 块中重新抛出第二个错误。
<html>
<head>
<title> Use of the throw keyword in JavaScript. </title>
</head>
<body>
<h2> Rethrowing the exception using throw Statement </h2>
<h4> Program execution flow, when we use the throw keyword to throw an user defined exception. </h4>
<div id="value"></div>
<script>
let value = document.getElementById("value");
try {
value.innerHTML += "control is inside the try block. </br>";
let a = 20;
let b = 30;
if (a + b > 30) {
throw "large number";
} else if (a + b < 30) {
throw "small number";
}
value.innerHTML += "terminating the try block. <br/>";
} catch (error) {
// handling the exception.
value.innerHTML += "inside the catch block. <br/>";
if (error == "large number") {
value.innerHTML += "handling the large number. <br/>";
} else {
throw "small number exception."
}
}
</script>
</body>
</html>结论
我们已经看到了在不同场景中使用 throw 关键字的示例。用户可以根据需要创建用户定义的异常并抛出错误消息。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP