ALL is Well!

          敏捷是一條很長的路,摸索著前進著

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            30 隨筆 :: 23 文章 :: 71 評論 :: 0 Trackbacks

          關于try-catch-finally的用法我就不多說了。網上搜一下,資料很多。

          這里我想主要討論下在finally塊加入return語句屏蔽異常的問題。

          我們先來看這樣一段代碼,


          代碼1

           1public class FinallyTest {   
           2    public static void main(String[] args) {   
           3        new FinallyTest().print();   
           4    }
             
           5    public void print() {   
           6        int i = -1;   
           7        try {   
           8            Thread.sleep(1);   
           9            i = 1 / 0;   
          10        }
           catch (Exception e) {   
          11            System.out.println("at catch block step 1.");   
          12            throw e;   
          13        }
           finally {   
          14            System.out.println("at finally block i = " + i);   
          15        }
             
          16    }
             
          17}


          以上代碼在Eclipse里是不會編譯通過的,因為在catch塊中throw了一個異常,而print方法并沒有聲明要拋出異常。


          現在我們修改代碼,讓它能夠通過編譯,代碼2

          1public class FinallyTest {   
          2    public static void main(String[] args) throws Exception {   
          3        new FinallyTest().print();   
          4    }
             
          5    public void print() throws Exception {   
          6…  
          7


          就是在printmain方法后加throws Exception,然后運行,看運行結果:

          1at catch block step 1.   
          2at finally block i = -1  
          3Exception in thread "main" java.lang.ArithmeticException: / by zero   
          4    at wxhx.csdn2.FinallyTest.print(FinallyTest.java:12)   
          5    at wxhx.csdn2.FinallyTest.main(FinallyTest.java:5)  

          程序先是走到了catch塊的第一句,打印了at catch block step 1.

          但并沒有緊接著去throw e即沒有立刻拋出這個異常,之所以這樣說,是因為異常的信息是在finally塊的打印信息之后才打印的。

          這個例子告訴我們,finally不管出異常與否,都必行的代(如果中途終止了jvm,就不必去執行了)

          那么何時執行finally塊中的代碼呢?

          在這個例子中,try塊中有異常拋出,所以finally塊中的代碼是在執行了catch語句之后、退出方法之前被執行的(如果這里執行了throw e,則方法就退出了)


           

          下面再看另外一個代碼,代碼3

           1public class FinallyTest {   
           2    public static void main(String[] args) {   
           3        new FinallyTest().print();   
           4    }
             
           5    public void print() {   
           6        int i = -1;   
           7        try {   
           8            Thread.sleep(1);   
           9            i = 1 / 0;   
          10        }
           catch (Exception e) {   
          11            System.out.println("at catch block step 1.");   
          12            throw e;   
          13        }
           finally {   
          14            System.out.println("at finally block i = " + i);   
          15            return;   
          16        }
             
          17    }
             
          18}
           


           

          這段代碼與之前相比,在finally塊中增加了return語句。

          雖然在catch塊中有throw e語句,但在print方法后并不用聲明throws Exception,也可以通過編譯。

          因為在try塊中有Thread.sleep(1);語句,所以必須要捕獲InterruptedException,但在這種情況下,即使我們把catch塊去掉了,也不會有問題,這是怎么回事呢?

          因為在finally塊中的return語句屏蔽了異常。

          經過代碼2我們已經知道了,異常在finally塊被執行之前,雖然會執行catch塊中的代碼,但并不會退出方法,在退出方法之前,會轉向finally塊中執行,而在finally塊中又恰好有return語句,所以方法就正常退出了,在try塊中產生的異常就不會有機會被拋出。


          ----2009年02月13日

          posted on 2010-09-01 12:00 李 明 閱讀(722) 評論(0)  編輯  收藏 所屬分類: J2SE技術知識
          主站蜘蛛池模板: 威信县| 彩票| 平顶山市| 济阳县| 三河市| 威远县| 泾源县| 甘孜| 米林县| 玛沁县| 清镇市| 永定县| 应城市| 木兰县| 本溪市| 南木林县| 伊宁市| 丰都县| 揭阳市| 大庆市| 长治市| 铁岭县| 两当县| 夏津县| 黄大仙区| 凤翔县| 抚顺县| 伊川县| 毕节市| 子长县| 康平县| 崇义县| 澄迈县| 特克斯县| 枣强县| 革吉县| 镇坪县| 南岸区| 通山县| 珲春市| 喀什市|