如何在 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 關鍵字的示例。使用者可以根據需要建立使用者定義的異常並丟擲錯誤訊息。

更新於:2022年7月20日

499 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告