1、try塊中沒有拋出異常,try、catch和finally塊中都有return語句 1 public static int NoException(){ 2 int i=10; 3 try{ 4 System.out.println("i in try block is:"+i); 5 retu ...
1、try塊中沒有拋出異常,try、catch和finally塊中都有return語句
1 public static int NoException(){ 2 int i=10; 3 try{ 4 System.out.println("i in try block is:"+i); 5 return --i; 6 } 7 catch(Exception e){ 8 --i; 9 System.out.println("i in catch - form try block is:"+i); 10 return --i; 11 } 12 finally{ 13 System.out.println("i in finally - from try or catch block is:"+i); 14 return --i; 15 } 16 }運行代碼:
1 public static void main(String[] args) { 2 System.out.println("=============NoException=================="); 3 System.out.println(NoException()); 4 System.out.println("==============================="); 5 }運行結果:
1 =============NoException================== 2 i in try block is:10 3 i in finally - from try or catch block is:9 4 8 5 ===============================執行順序: 執行try塊,執行到return語句時,先執行return的語句,--i,但是不返回到main方法,執行finally塊,遇到finally塊中的return語句,執行--i,並將值返回到main方法,這裡就不會再回去返回try塊中計算得到的值。 結論:try-catch-finally都有return語句時,沒有異常時,返回值是finally中的return返回的。 2.try塊中沒有拋出異常,僅try和catch中有return語句
1 public static int NoException1(){ 2 int i=10; 3 try{ 4 System.out.println("i in try block is:"+i); 5 return --i; 6 } 7 catch(Exception e){ 8 --i; 9 System.out.println("i in catch - form try block is:"+i); 10 return --i; 11 } 12 finally{ 13 System.out.println("i in finally - from try or catch block is:"+i); 14 --i; 15 System.out.println("i in finally block is:"+i); 16 //return --i; 17 } 18 }運行結果:
1 =============NoException1================== 2 i in try block is:10 3 i in finally - from try or catch block is:9 4 i in finally block is:8 5 9 6 ===============================執行順序: try中執行完return的語句後,不返回,執行finally塊,finally塊執行結束後,返回到try塊中,返回i在try塊中最後的值。 結論:try-catch都有return語句時,沒有異常時,返回值是try中的return返回的。 3.try塊中拋出異常,try、catch和finally中都有return語句
1 public static int WithException(){ 2 int i=10; 3 try{ 4 System.out.println("i in try block is:"+i); 5 i = i/0; 6 return --i; 7 } 8 catch(Exception e){ 9 System.out.println("i in catch - form try block is:"+i); 10 --i; 11 System.out.println("i in catch block is:"+i); 12 return --i; 13 } 14 finally{ 15 System.out.println("i in finally - from try or catch block is--"+i); 16 --i; 17 System.out.println("i in finally block is--"+i); 18 return --i; 19 } 20 }執行結果:
1 =============WithException================== 2 i in try block is:10 3 i in catch - form try block is:10 4 i in catch block is:9 5 i in finally - from try or catch block is--8 6 i in finally block is--7 7 6 8 ===============================執行順序: 拋出異常後,執行catch塊,在catch塊的return的--i執行完後,並不直接返回而是執行finally,因finally中有return語句,所以,執行,返回結果6。 結論: try塊中拋出異常,try、catch和finally中都有return語句,返回值是finally中的return。 4.try塊中拋出異常,try和catch中都有return語句
1 public static int WithException1(){ 2 int i=10; 3 try{ 4 System.out.println("i in try block is:"+i); 5 i=i/0; 6 return --i; 7 }catch(Exception e){ 8 System.out.println("i in catch - form try block is:"+i); 9 return --i; 10 }finally{ 11 12 System.out.println("i in finally - from try or catch block is:"+i); 13 --i; 14 System.out.println("i in finally block is:"+i); 15 //return i; 16 } 17 }執行結果:
1 =============WithException1================== 2 i in try block is:10 3 i in catch - form try block is:10 4 i in finally - from try or catch block is:9 5 i in finally block is:8 6 9 7 ===============================執行順序: 拋出異常後,執行catch塊,執行完finally語句後,依舊返回catch中的執行return語句後的值,而不是finally中修改的值。 結論: 返回的catch中return值。 5.try、catch中都出現異常,在finally中有返回
1 public static int WithException2(){ 2 int i=10; 3 try{ 4 System.out.println("i in try block is:"+i); 5 i=i/0; 6 return --i; 7 } 8 catch(Exception e){ 9 System.out.println("i in catch - form try block is:"+i); 10 int j = i/0; 11 return --i; 12 } 13 finally{ 14 15 System.out.println("i in finally - from try or catch block is:"+i); 16 --i; 17 --i; 18 System.out.println("i in finally block is:"+i); 19 return --i; 20 }執行結果:
1 =============WithException2================== 2 i in try block is:10 3 i in catch - form try block is:10 4 i in finally - from try or catch block is:10 5 i in finally block is:8 6 7 7 ===============================執行順序: try塊中出現異常到catch,catch中出現異常到finally,finally中執行到return語句返回,不檢查異常。 結論: 返回finally中return值。 6、只在函數最後出現return語句
1 public static int WithException3(){ 2 int i=10; 3 try{ 4 System.out.println("i in try block is:"+i); 5 i=i/0; 6 //return --i; 7 } 8 catch(Exception e){ 9 System.out.println("i in catch - form try block is:"+i); 10 //int j = i/0; 11 //return --i; 12 } 13 finally{ 14 15 System.out.println("i in finally - from try or catch block is:"+i); 16 --i; 17 --i; 18 System.out.println("i in finally block is:"+i); 19 //return --i; 20 } 21 return --i; 22 }執行結果:
1 =============WithException3================== 2 i in try block is:10 3 i in catch - form try block is:10 4 i in finally - from try or catch block is:10 5 i in finally block is:8 6 7 7 ===============================總體結論: 結論一: return語句並不是函數的最終出口,如果有finally語句,這在return之後還會執行finally(return的值會暫存在棧裡面,等待finally執行後再返回) 結論二: finally裡面不建議放return語句,根據需要,return語句可以放在try和catch裡面和函數的最後。可行的做法有四: (1)return語句只在函數最後出現一次。 (2)return語句僅在try和catch裡面都出現。 (3)return語句僅在try和函數的最後都出現。 (4)return語句僅在catch和函數的最後都出現。 註意,除此之外的其他做法都是不可行的,編譯器會報錯。 轉載自:http://qing0991.blog.51cto.com/1640542/1387200