關於try catch finally,trycatchfinally

來源:互聯網
上載者:User

關於try catch finally,trycatchfinally
一、try catch finally的初步理解

  首先,我們什麼時候要用到try/catch和finally?  ——已知執行的代碼有可能有異常的情況下

    try { //這裡執行的代碼如果出現異常,則立即跳到catch執行。} 

    catch { //除非try裡面執行代碼發生了異常,否則這裡的代碼不會執行 } 

    finally { //不管有沒有異常,finally中的代碼都會執行,可以理解為只要執行了try或者catch,就一定會執行 finally } 

二、try catch finally的執行順序(無return)

    1.我們來看段代碼

 1 public class Test01 { 2     public static void main(String[] args) { 3         int i = 1; 4         test01(i); 5     } 6      7     public static void test01(int i) { 8         try { 9             i++;10             System.out.println("try-----"+i);11             i=1/0;//製造異常句子12             i++;13             System.out.println("try2-----"+i);14         } catch (Exception e) {15             i++;16             System.out.println("catch-----"+i);17         }finally {18             i=5;19             System.out.println("finally-----"+i);20         }21     }22 }

    2.控制台輸出結果為:

      

 

    3.總結:從輸出結果可以看出,執行順序為:try{} catch{} finally{} ;

      也就是先執行try{}(try-----2),try裡面發生異常(i=i/0;),跳去執行catch(catch-----3),不管有沒有發生異常都會執行的finally(finally-----5);

      (如果try裡面沒有異常將不執行catch,這裡就不予以示範了)

三、try catch finally的執行順序(帶return),這裡有兩種情況

  第一種,return在try catch finally 後面

    1.我們來看段代碼

 1 public class Test01 { 2     public static void main(String[] args) { 3         int i = 1; 4         i=test01(i); 5         System.out.println("main--------"+i); 6     } 7      8     public static int test01(int i) { 9         try {10             i++;11             System.out.println("try-----"+i);12         } catch (Exception e) {13             i++;14             System.out.println("catch----"+i);15         }finally {16             i++;17             System.out.println("finally---"+i);18         }19         return i;20     }21 }

    2.控制台輸出結果

      

 

    3.總結:從輸出結果可以看出,執行順序為:try{} /catch{} finally{} return;

      在這裡沒有製造異常,就正常執行,“i”的初始值是“1”,在try裡面“i++”--這時候“i”的值是“2”,在到finally裡面執行“i++”,這時候“i”的值是“3”;

      最後返回給方法返回的是“3”。

      這裡也進一步說明了無論try{}裡面有沒有發生異常,finally{}裡面的代碼都會執行

  第二種,return在try catch 裡面——重點

    1.我們來看段代碼

 1 public class Test01 { 2     public static void main(String[] args) { 3         int i = 1; 4         i=test01(i); 5         System.out.println("main--------"+i); 6     } 7      8     public static int test01(int i) { 9         try {10             i++;11             System.out.println("try-----"+i);12             return i;13         } catch (Exception e) {14             i++;15             System.out.println("catch----"+i);16             return i;17         }finally {18             i=5;19             System.out.println("finally---"+i);20         }21     }22 }

    2.控制台輸出結果為

      

    3.總結:從輸出結果可以看出,執行順序依然是:try{} /catch{} finally{} return;

      ①finally是無論如何都會執行的(列印了fianlly),所以就算try裡面有return語句也不會直接返回。

      ②執行了finally之後,按道理來說“i”的值應該是“5”,為什麼最後返回給main方法的值是“2”呢:

        個人猜測:這裡返回的"i"不是原來的變數“i”,而是系統另外為try裡面的“i值”定義了一個“i2”,返回的是“i2”,但是在這個時候“i”的值還是finally裡面定義的“5”,然後在main方法裡面又把“i2值”賦給了“i”,這時候"i"亦為“2”;

    4.注意:如果在finally裡面添加return語句,那麼將會直接返回fianlly裡面的“i”,這時候main裡面輸出的“i”值為“5”。

      這也是進一步的說明了“不管有沒有異常,finally中的代碼都會執行”;那finally裡面的代碼執行了之後,便也執行了return語句,將不再執行try裡面的return語句;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.