如何在 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 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告