<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 如何在 java 中拋出異常 > 原文: [https://beginnersbook.com/2013/04/throw-in-java/](https://beginnersbook.com/2013/04/throw-in-java/) 在 Java 中,我們已經定義了異常類,例如`ArithmeticException`,`NullPointerException`,`ArrayIndexOutOfBounds`異常等。這些異常被設置為在不同的條件下觸發。例如,當我們將一個數除以零時,這會觸發`ArithmeticException`,當我們嘗試從其邊界中訪問數組元素時,我們得到`ArrayIndexOutOfBoundsException`。 我們可以定義自己的條件或規則集,并使用`throw`關鍵字顯式拋出異常。例如,當我們將數字除以 5 或任何其他數字時,我們可以拋出`ArithmeticException`,我們需要做的只是設置條件并使用`throw`關鍵字拋出任何異常。`Throw`關鍵字也可用于拋出自定義異常,我已在單獨的教程中介紹過,請參閱 Java 中的[自定義異常](https://beginnersbook.com/2013/04/user-defined-exception-in-java/)。 **`throw`關鍵字語法:** ```java throw new exception_class("error message"); ``` 例如: ```java throw new ArithmeticException("dividing a number by 5 is not allowed in this program"); ``` ## `throw`關鍵字的示例 假設我們有一個要求,我們只需要在年齡小于 12 且體重小于 40 的情況下注冊學生,如果不滿足任何條件,那么用戶應該獲得帶有警告消息“學生沒有資格注冊”的`ArithmeticException`。我們已經通過將代碼放在檢查學生資格的方法中來實現邏輯,如果輸入的學生年齡和體重不符合標準,那么我們使用`throw`關鍵字拋出異常。 ```java /* In this program we are checking the Student age * if the student age<12 and weight <40 then our program * should return that the student is not eligible for registration. */ public class ThrowExample { static void checkEligibilty(int stuage, int stuweight){ if(stuage<12 && stuweight<40) { throw new ArithmeticException("Student is not eligible for registration"); } else { System.out.println("Student Entry is Valid!!"); } } public static void main(String args[]){ System.out.println("Welcome to the Registration process!!"); checkEligibilty(10, 39); System.out.println("Have a nice day.."); } } ``` 輸出: ```java Welcome to the Registration process!!Exception in thread "main" java.lang.ArithmeticException: Student is not eligible for registration at beginnersbook.com.ThrowExample.checkEligibilty(ThrowExample.java:9) at beginnersbook.com.ThrowExample.main(ThrowExample.java:18) ``` 在上面的例子中,我們拋出了一個非受檢的異常,同樣地,我們可以拋出[非受檢](https://beginnersbook.com/2013/04/java-checked-unchecked-exceptions-with-examples/)和[用戶定義的異常](https://beginnersbook.com/2013/04/user-defined-exception-in-java/)。 有關`throw`關鍵字的更多示例,請參閱:[`throw`關鍵字示例](https://beginnersbook.com/2013/04/throw-in-java/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看