<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 `finally`塊 - 異常處理 > 原文: [https://beginnersbook.com/2013/04/java-finally-block/](https://beginnersbook.com/2013/04/java-finally-block/) 在之前的教程中,我介紹了[`try-catch`塊](https://beginnersbook.com/2013/04/try-catch-in-java/)和[嵌套的`try`塊](https://beginnersbook.com/2013/04/nested-try-catch/)。在本指南中,我們將看到`finally try`與`try-catch`一起使用。 **`finally`塊**包含所有必須執行的關鍵語句,無論是否發生異常。無論 try 塊是否發生異常,例如關閉連接,流等,此塊中的語句將始終執行。 ## `finally`塊的語法 ```java try { //Statements that may cause an exception } catch { //Handling exception } finally { //Statements to be executed } ``` ## `finally`塊的簡單示例 在這里你可以看到異常發生在`try`塊中,它已經在`catch`塊中被處理,在`finally`塊被執行之后。 ```java class Example { public static void main(String args[]) { try{ int num=121/0; System.out.println(num); } catch(ArithmeticException e){ System.out.println("Number should not be divided by zero"); } /* Finally block will always execute * even if there is no exception in try block */ finally{ System.out.println("This is finally block"); } System.out.println("Out of try-catch-finally"); } } ``` **輸出:** ```java Number should not be divided by zero This is finally block Out of try-catch-finally ``` ## `finally`塊的幾個重點 1. `finally`塊必須與`try`塊相關聯,如果沒有`try`塊,則不能使用`finally`塊。您應該將這些語句放在必須始終執行的塊中。 2. `finally`塊是可選的,正如我們在前面的教程中看到的那樣,`try-catch`塊足以用于[異常處理](https://beginnersbook.com/2013/04/java-exception-handling/),但是如果你放置一個`finally`塊,那么它總是在執行`try`塊后運行。 3. 在正常情況下,當`try`塊中沒有異常時,則在`try`塊之后執行`finally`塊。但是,如果發生異常,則在`finally`塊之前執行`catch`塊。 4. `finally`塊中的異常行為與任何其他異常完全相同。 5. 即使`try`塊包含諸如`return`,`break`或`continue`之類的控制轉移語句,`finally`塊中的語句最終也會執行。 讓我們看一個例子,看看當`try`塊中存在`return`語句時最終是如何工作的: ### `finally`塊和`return`語句的另一個例子 你可以看到,即使我們在方法中有`return`語句,`finally`塊仍然會運行。 ```java class JavaFinally { public static void main(String args[]) { System.out.println(JavaFinally.myMethod()); } public static int myMethod() { try { return 112; } finally { System.out.println("This is Finally block"); System.out.println("Finally block ran even after return statement"); } } } ``` **以上程序的輸出:** ```java This is Finally block Finally block ran even after return statement 112 ``` 要查看`finally`和`return`的更多示例,請參閱: [Java `finally`塊和返回語句](https://beginnersbook.com/2013/05/java-finally-return/)。 ## `finally`塊未執行時的情況 阻止在`finally`塊中執行代碼的情況是: - 線程的死亡 - 使用`System.exit()`方法。 - 由于`finally`塊中出現異常。 ## `finally`和`close()` `close()`語句用于關閉程序中的所有打開流。在`finally`塊中使用`close()`是一個很好的做法。由于即使發生異常,最終塊也會執行,因此無論是否發生異常,您都可以確保所有輸入和輸出流都已正確關閉。 例如: ```java .... try{ OutputStream osf = new FileOutputStream( "filename" ); OutputStream osb = new BufferedOutputStream(opf); ObjectOutput op = new ObjectOutputStream(osb); try{ output.writeObject(writableObject); } finally{ op.close(); } } catch(IOException e1){ System.out.println(e1); } ... ``` ## 沒有`catch`的`finally`塊 可以在沒有`catch`塊的情況下使用`try-finally`塊。這意味著`try`塊可以在沒有`catch`塊的情況下使用。 ```java ... InputStream input = null; try { input = new FileInputStream("inputfile.txt"); } finally { if (input != null) { try { in.close(); }catch (IOException exp) { System.out.println(exp); } } } ... ``` ## `finally`塊和`System.exit()` `System.exit()`語句的行為與`return`語句不同。與`return`語句不同,每當在`try`塊中調用`System.exit()`時,`finally`塊不會執行。這是一個代碼片段,演示了相同的代碼: ```java .... try { //try block System.out.println("Inside try block"); System.exit(0) } catch (Exception exp) { System.out.println(exp); } finally { System.out.println("Java finally block"); } .... ``` 在上面的例子中,如果`System.exit(0)`被調用而沒有任何異常,那么最終將不會執行。但是,如果在調用`System.exit(0)`時發生任何異常,則將執行`finally`塊。 ## `try-catch-finally`塊 * `try`語句應該與`catch`塊或`finally`相關聯。 * 由于`catch`執行異常處理并最終執行清理,因此最好的方法是同時使用它們。 **語法:** ```java try { //statements that may cause an exception } catch (…)? { //error handling code } finally { //statements to be executed } ``` ### `try-catch-finally`塊的例子 **示例 1:**以下示例演示了`try`塊中沒有異常時`finally`塊的工作情況 ```java class Example1{ public static void main(String args[]){ try{ System.out.println("First statement of try block"); int num=45/3; System.out.println(num); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("ArrayIndexOutOfBoundsException"); } finally{ System.out.println("finally block"); } System.out.println("Out of try-catch-finally block"); } } ``` **輸出:** ```java First statement of try block 15 finally block Out of try-catch-finally block ``` **示例 2:**此示例顯示了在`try`塊中發生異常但在`catch`塊中未處理時`finally`塊的工作: ```java class Example2{ public static void main(String args[]){ try{ System.out.println("First statement of try block"); int num=45/0; System.out.println(num); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("ArrayIndexOutOfBoundsException"); } finally{ System.out.println("finally block"); } System.out.println("Out of try-catch-finally block"); } } ``` **輸出:** ```java First statement of try block finally block Exception in thread "main" java.lang.ArithmeticException: / by zero at beginnersbook.com.Example2.main(Details.java:6) ``` 正如您所看到的那樣,系統生成了異常消息,但在此之前,`finally`塊已成功執行。 **例 3** :當`try`塊發生異常并在`catch`塊中正確處理時 ```java class Example3{ public static void main(String args[]){ try{ System.out.println("First statement of try block"); int num=45/0; System.out.println(num); } catch(ArithmeticException e){ System.out.println("ArithmeticException"); } finally{ System.out.println("finally block"); } System.out.println("Out of try-catch-finally block"); } } ``` **輸出:** ```java First statement of try block ArithmeticException finally block Out of try-catch-finally block ```
                  <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>

                              哎呀哎呀视频在线观看