<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之旅 廣告
                ### [finally 用來做什么?](https://lingcoder.gitee.io/onjava8/#/book/15-Exceptions?id=finally-%e7%94%a8%e6%9d%a5%e5%81%9a%e4%bb%80%e4%b9%88%ef%bc%9f) 對于沒有垃圾回收和析構函數自動調用機制的語言來說,finally 非常重要。它能使程序員保證:無論 try 塊里發生了什么,內存總能得到釋放。但 Java 有垃圾回收機制,所以內存釋放不再是問題。而且,Java 也沒有析構函數可供調用。那么,Java 在什么情況下才能用到 finally 呢? 當要把除內存之外的資源恢復到它們的初始狀態時,就要用到 finally 子句。這種需要清理的資源包括:已經打開的文件或網絡連接,在屏幕上畫的圖形,甚至可以是外部世界的某個開關,如下面例子所示: ~~~ // exceptions/Switch.java public class Switch { private boolean state = false; public boolean read() { return state; } public void on() { state = true; System.out.println(this); } public void off() { state = false; System.out.println(this); } @Override public String toString() { return state ? "on" : "off"; } } // exceptions/OnOffException1.java public class OnOffException1 extends Exception {} // exceptions/OnOffException2.java public class OnOffException2 extends Exception {} // exceptions/OnOffSwitch.java // Why use finally? public class OnOffSwitch { private static Switch sw = new Switch(); public static void f() throws OnOffException1, OnOffException2 {} public static void main(String[] args) { try { sw.on(); // Code that can throw exceptions... f(); sw.off(); } catch(OnOffException1 e) { System.out.println("OnOffException1"); sw.off(); } catch(OnOffException2 e) { System.out.println("OnOffException2"); sw.off(); } } } ~~~ 輸出為: ~~~ on off ~~~ 程序的目的是要確保 main() 結束的時候開關必須是關閉的,所以在每個 try 塊和異常處理程序的末尾都加入了對 sw.off() 方法的調用。但也可能有這種情況:異常被拋出,但沒被處理程序捕獲,這時 sw.off() 就得不到調用。但是有了 finally,只要把 try 塊中的清理代碼移放在一處即可: ~~~ // exceptions/WithFinally.java // Finally Guarantees cleanup public class WithFinally { static Switch sw = new Switch(); public static void main(String[] args) { try { sw.on(); // Code that can throw exceptions... OnOffSwitch.f(); } catch(OnOffException1 e) { System.out.println("OnOffException1"); } catch(OnOffException2 e) { System.out.println("OnOffException2"); } finally { sw.off(); } } } ~~~ 輸出為: ~~~ on off ~~~ 這里 sw.off() 被移到一處,并且保證在任何情況下都能得到執行。 甚至在異常沒有被當前的異常處理程序捕獲的情況下,異常處理機制也會在跳到更高一層的異常處理程序之前,執行 finally 子句: ~~~ // exceptions/AlwaysFinally.java // Finally is always executed class FourException extends Exception {} public class AlwaysFinally { public static void main(String[] args) { System.out.println("Entering first try block"); try { System.out.println("Entering second try block"); try { throw new FourException(); } finally { System.out.println("finally in 2nd try block"); } } catch(FourException e) { System.out.println( "Caught FourException in 1st try block"); } finally { System.out.println("finally in 1st try block"); } } } ~~~ 輸出為: ~~~ Entering first try block Entering second try block finally in 2nd try block Caught FourException in 1st try block finally in 1st try block ~~~ 當涉及 break 和 continue 語句的時候,finally 子句也會得到執行。請注意,如果把 finally 子句和帶標簽的 break 及 continue 配合使用,在 Java 里就沒必要使用 goto 語句了。
                  <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>

                              哎呀哎呀视频在线观看