<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # java 中的用戶定義的異常 > 原文: [https://beginnersbook.com/2013/04/user-defined-exception-in-java/](https://beginnersbook.com/2013/04/user-defined-exception-in-java/) 在 java 中我們已經定義了異常類,例如`ArithmeticException`,`NullPointerException`等。這些異常已經設置為在預定義條件下觸發,例如當你將數字除以零時它會觸發`ArithmeticException`,在上一個教程中我們學習了如何拋出這些異常根據您使用[`throw`關鍵字](https://beginnersbook.com/2013/04/throw-in-java/)的條件明確顯示。 在 java 中,我們可以創建自己的異常類,并使用`throw`關鍵字拋出該異常。這些異常稱為**用戶定義的**或**自定義**異常。在本教程中,我們將了解如何創建自己的自定義異常并將其拋出到特定條件。 要理解本教程,您應該具備[`try-catch`塊](https://beginnersbook.com/2013/04/try-catch-in-java/)和[ java 中的`throw`](https://beginnersbook.com/2013/04/throw-in-java/) 的基本知識。 ## Java 中用戶定義的異常示例 ```java /* This is my Exception class, I have named it MyException * you can give any name, just remember that it should * extend Exception class */ class MyException extends Exception{ String str1; /* Constructor of custom exception class * here I am copying the message that we are passing while * throwing the exception to a string and then displaying * that string along with the message. */ MyException(String str2) { str1=str2; } public String toString(){ return ("MyException Occurred: "+str1) ; } } class Example1{ public static void main(String args[]){ try{ System.out.println("Starting of try block"); // I'm throwing the custom exception using throw throw new MyException("This is My error Message"); } catch(MyException exp){ System.out.println("Catch Block") ; System.out.println(exp) ; } } } ``` **輸出:** ```java Starting of try block Catch Block MyException Occurred: This is My error Message ``` **說明:** 你可以看到,在拋出自定義異常時,我在括號中給出了一個字符串(`throw new MyException("This is My error Message");`)。這就是為什么我的自定義異常類中有[參數化構造函數](https://beginnersbook.com/2014/01/parameterized-constructor-in-java-example/)(帶有`String`參數)的原因。 **注意:** 1. 用戶定義的異常必須擴展`Exception`類。 2. 使用`throw`關鍵字拋出異常。 ## 自定義異常的另一個例子 在這個例子中,我們從方法中拋出一個異常。在這種情況下,我們應該在方法簽名中使用`throws`子句,否則你將得到編譯錯誤,說“方法中未處理的異常”。要了解`throws`子句的工作原理,請參考本指南:[java 中的`throw`關鍵字](https://beginnersbook.com/2013/04/java-throws/)。 ```java class InvalidProductException extends Exception { public InvalidProductException(String s) { // Call constructor of parent Exception super(s); } } public class Example1 { void productCheck(int weight) throws InvalidProductException{ if(weight<100){ throw new InvalidProductException("Product Invalid"); } } public static void main(String args[]) { Example1 obj = new Example1(); try { obj.productCheck(60); } catch (InvalidProductException ex) { System.out.println("Caught the exception"); System.out.println(ex.getMessage()); } } } ``` **輸出:** ```java Caught the exception Product Invalid ```
                  <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>

                              哎呀哎呀视频在线观看