如何在 JavaScript 中使用 throw 语句?


在本教程中,我们将学习如何在 JavaScript 中使用 throw 语句。**“throw”** 是 JavaScript 中的保留关键字,程序员可以使用 throw 关键字来创建**用户定义的异常**。

每个程序员都不是完美的,因此他们无法在编写 JavaScript 代码时不犯任何错误。也可能发生程序员从用户那里获取输入,如果用户输入无效,则会发生异常。一些内置异常包括算术异常、索引越界异常等。在某些情况下,程序员希望创建自己的异常,并且可以使用 try…catch 块中的 throw 语句来做到这一点。

下面,我们提供了使用 throw 关键字在 JavaScript 中的不同示例。

使用 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 语句创建新的错误对象

在上一节中,我们创建了简单的错误消息来抛出异常,但在本节中,我们将创建新的**用户定义错误对象**。JavaScript 有一个名为 error 的内置类来管理异常。此外,我们可以创建 error 类的对象实例并将错误消息作为参数传递。

语法

用户可以在下面看到抛出新错误对象的语法。

try {
   // program code
   if ( condition ){
      throw new Error("message");
   }
} catch (error) {
   // handle user-defined exception.
}

示例

在下面的示例中,我们正在检查变量的类型。如果变量类型未定义,我们将抛出并使用带错误消息的用户定义错误对象。因此,我们可以在 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 关键字的示例。用户可以创建用户定义的异常并根据需要抛出错误消息。

更新于: 2022年7月20日

499 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.