<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 `throw` > 原文: [https://www.programiz.com/java-programming/throw-throws](https://www.programiz.com/java-programming/throw-throws) #### 在本教程中,我們將在示例的幫助下學習使用`throw`和`throws`關鍵字進行異常處理。 在 Java 中,異常可以分為兩種類型: * **非受檢的異常**:它們不是在編譯時檢查的,而是在運行時檢查的。例如:`ArithmeticException`,`NullPointerException`,`ArrayIndexOutOfBoundsException`,`Error`類下的異常等。 * **受檢的異常**:在編譯時檢查它們。 例如`IOException`,`InterruptedException`等。 請參閱 [Java 異常](https://www.programiz.com/java-programming/exceptions),以詳細了解已檢查和非受檢的異常。 通常,我們不需要處理非受檢的異常。 這是因為由于編程錯誤而發生了非受檢的異常。 并且,糾正它們而不是處理它們是一個好習慣。 現在,本教程將重點介紹如何使用`throw`和`throws`處理受檢的異常。 * * * ## Java `throws`關鍵字 我們在方法聲明中使用`throws`關鍵字來聲明其中可能發生的異常的類型。 其語法為: ```java accessModifier returnType methodName() throws ExceptionType1, ExceptionType2 … { // code } ``` 從上面的語法可以看到,我們可以使用`throws`聲明多個異常。 * * * ### 示例 1:Java `throws`關鍵字 ```java import java.io.*; class Main { public static void findFile() throws IOException { // code that may produce IOException File newFile=new File("test.txt"); FileInputStream stream=new FileInputStream(newFile); } public static void main(String[] args) { try{ findFile(); } catch(IOException e){ System.out.println(e); } } } ``` **輸出** ```java java.io.FileNotFoundException: test.txt (No such file or directory) ``` 當我們運行該程序時,如果文件`test.txt`不存在,則`FileInputStream`會拋出`FileNotFoundException`,該文件擴展了`IOException`類。 如果某個方法不處理異常,則必須在`throws`子句中指定其中可能發生的異常的類型,以便調用棧中更深的方法可以處理它們或使用`throws`關鍵字自己指定它們。 `findFile()`方法指定可以拋出`IOException`。`main()`方法調用此方法并處理拋出的異常。 * * * ### 引發多個異常 這是我們使用`throws`關鍵字引發多個異常的方法。 ```java import java.io.*; class Main { public static void findFile() throws NullPointerException, IOException, InvalidClassException { // code that may produce NullPointerException … … … // code that may produce IOException … … … // code that may produce InvalidClassException … … … } public static void main(String[] args) { try{ findFile(); } catch(IOException e1){ System.out.println(e1.getMessage()); } catch(InvalidClassException e2){ System.out.println(e2.getMessage()); } } } ``` 在這里,`findFile()`方法指定它可以在其`throws`子句中拋出`NullPointerException`,`IOException`和`InvalidClassException`。 請注意,我們尚未處理`NullPointerException`。 這是因為它是非受檢的異常。 不必在`throws`子句中指定它并進行處理。 * * * ### `throws`關鍵字 Vs `try-catch-finally` 可能有幾種方法可能導致異常。 為每種方法編寫`try...catch`將會很繁瑣,并且代碼變得冗長且可讀性較差。 當您檢查了不想在當前方法中捕獲的異常(必須處理的異常)時,`throws`也很有用。 * * * ## Java `throw`關鍵字 `throw`關鍵字用于顯式引發單個異常。 引發異常時,程序執行流程從`try`塊轉移到`catch`塊。 我們在方法中使用`throw`關鍵字。 它的語法是: ```java throw throwableObject; ``` 可拋出對象是`Throwable`類或`Throwable`類的子類的實例。 * * * ### 示例 2:Java `throw`關鍵字 ```java class Main { public static void divideByZero() { throw new ArithmeticException("Trying to divide by 0"); } public static void main(String[] args) { divideByZero(); } } ``` **輸出**: ```java Exception in thread "main" java.lang.ArithmeticException: Trying to divide by 0 at Main.divideByZero(Main.java:3) at Main.main(Main.java:7) exit status 1 ``` 在此示例中,我們明確拋出了`ArithmeticException.` **注意**: `ArithmeticException`是非受檢的異常。 通常沒有必要處理非受檢的異常。 * * * ### 示例 3:引發受檢異常 ```java import java.io.*; class Main { public static void findFile() throws IOException { throw new IOException("File not found"); } public static void main(String[] args) { try { findFile(); System.out.println("Rest of code in try block"); } catch (IOException e) { System.out.println(e.getMessage()); } } } ``` **輸出**: ```java File not found ``` `findFile()`方法會向我們傳遞給其構造器的消息引發`IOException`。 注意,由于它是一個受檢的異常,因此必須在`throws`子句中指定它。 調用此`findFile()`方法的方法需要處理此異常,或者自己使用`throws`關鍵字指定該異常。 我們已經在`main ()`方法中處理了此異常。 引發異常時,程序執行流程從`try`塊轉移到`catch`塊。 因此,將跳過`try`塊中的其余代碼,并執行`catch`塊中的語句。
                  <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>

                              哎呀哎呀视频在线观看