try--catch--finally中return返回值執行的順序(區別)


1、try塊中沒有拋出異常,try、catch和finally塊中都有return語句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public  static  int  NoException(){
          int  i= 10 ;
          try {
            System.out.println( "i in try block is:" +i);
            return  --i;
          }
          catch (Exception e){
            --i;
            System.out.println( "i in catch - form try block is:" +i);
            return  --i;
          }
          finally {     
            System.out.println( "i in finally - from try or catch block is:" +i);
            return  --i;
          }  
}

運行代碼:

1
2
3
4
5
public  static  void  main( String [] args) {
         System.out.println( "=============NoException==================" );
         System.out.println(NoException());
         System.out.println( "===============================" );   
}

運行結果:

1
2
3
4
5
=============NoException==================
in  try  block  is 10
in  finally  - from  try  or  catch  block  is 9
8
===============================

 

執行順序:

   執行try塊,執行到return語句時,先執行return的語句,--i,但是不返回到main方法,執行finally塊,遇到finally塊中的return語句,執行--i,並將值返回到main方法,這里就不會再回去返回try塊中計算得到的值。

結論:try-catch-finally都有return語句時,沒有異常時,返回值是finally中的return返回的。從下面的字節碼可以看出,try里的return將被忽略。

 

2.try塊中沒有拋出異常,僅try和catch中有return語句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public  static  int  NoException1(){
             int  i= 10 ;
             try {
                 System.out.println( "i in try block is:" +i);
                 return  --i;
             }
             catch (Exception e){
                 --i;
                 System.out.println( "i in catch - form try block is:" +i);
                 return  --i;
             }
             finally {           
                 System.out.println( "i in finally - from try or catch block is:" +i);
                 --i;
                 System.out.println( "i in finally block is:" +i);
                 //return --i;
             }
}

運行結果:

1
2
3
4
5
6
=============NoException1==================
in  try  block  is 10
in  finally  - from  try  or  catch  block  is 9
in  finally  block  is 8
9
===============================

執行順序:

   try中執行完return的語句后,不返回,執行finally塊,finally塊執行結束后,返回到try塊中,返回i在try塊中最后的值。

結論:try-catch都有return語句時,沒有異常時,返回值是try中的return返回的。

通過字節碼,我們發現,在try語句的return塊中,return 返回的引用變量(i 是引用類型)並不是try語句外定義的引用變量i,而是系統重新定義了一個局部引用i'(29行標紅色框的,系統在這里面用第三個變量保存當前的值),這個引用指向了引用i對應的值,也就是try ,即使在finally語句中把引用i指向了值減1,因為return的返回引用已經不是i 而是臨時的iload_3,所以引用i的對應的值和try語句中的返回值無關了。 

3.try塊中拋出異常,try、catch和finally中都有return語句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public  static  int  WithException(){
             int  i= 10 ;
             try {
                 System.out.println( "i in try block is:" +i);
                 i = i/ 0 ;
                 return  --i;
             }
             catch (Exception e){
                 System.out.println( "i in catch - form try block is:" +i);
                 --i;
                 System.out.println( "i in catch block is:" +i);
                 return  --i;
             }
             finally {           
                 System.out.println( "i in finally - from try or catch block is--" +i);
                 --i;
                 System.out.println( "i in finally block is--" +i);
                 return  --i;
             }
}

執行結果:

1
2
3
4
5
6
7
8
=============WithException==================
in  try  block  is 10
in  catch  - form  try  block  is 10
in  catch  block  is 9
in  finally  - from  try  or  catch  block  is -- 8
in  finally  block  is -- 7
6
===============================

執行順序:

   拋出異常后,執行catch塊,在catch塊的return的--i執行完后,並不直接返回而是執行finally,因finally中有return語句,所以,執行,返回結果6。

結論:

   try塊中拋出異常,try、catch和finally中都有return語句,返回值是finally中的return。

 

4.try塊中拋出異常,try和catch中都有return語句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public  static  int  WithException1(){
             int  i= 10 ;
             try {
                 System.out.println( "i in try block is:" +i);
                 i=i/ 0 ;
                 return  --i;
             } catch (Exception e){
                 System.out.println( "i in catch - form try block is:" +i);           
                 return  --i;
             } finally {
                                                                                                                                                                      
                 System.out.println( "i in finally - from try or catch block is:" +i);
                 --i;
                 System.out.println( "i in finally block is:" +i);
                 //return i;
             }
}

執行結果:

1
2
3
4
5
6
7
=============WithException1==================
in  try  block  is 10
in  catch  - form  try  block  is 10
in  finally  - from  try  or  catch  block  is 9
in  finally  block  is 8
9
===============================

執行順序:

   拋出異常后,執行catch塊,執行完finally語句后,依舊返回catch中的執行return語句后的值,而不是finally中修改的值。

結論:

   返回的catch中return值。

 

5.try、catch中都出現異常,在finally中有返回

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public  static  int  WithException2(){
             int  i= 10 ;
             try {
                 System.out.println( "i in try block is:" +i);
                 i=i/ 0 ;
                 return  --i;
             }
             catch (Exception e){
                 System.out.println( "i in catch - form try block is:" +i);
                 int  j = i/ 0 ;
                 return  --i;
             }
             finally {
                                                                                       
                 System.out.println( "i in finally - from try or catch block is:" +i);
                 --i;
                 --i;
                 System.out.println( "i in finally block is:" +i);
                 return  --i;
}

執行結果:

1
2
3
4
5
6
7
=============WithException2==================
in  try block  is :10
in  catch - form try block  is :10
in  finally -  from  try  or  catch block  is :10
in  finally block  is :8
7
===============================

執行順序:   

   try塊中出現異常到catch,catch中出現異常到finally,finally中執行到return語句返回,不檢查異常。

結論:

   返回finally中return值。

6、只在函數最后出現return語句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public  static  int  WithException3(){
             int  i= 10 ;
             try {
                 System.out.println( "i in try block is:" +i);
                 i=i/ 0 ;
                 //return --i;
             }
             catch (Exception e){
                 System.out.println( "i in catch - form try block is:" +i);
                 //int j = i/0;
                 //return --i;
             }
             finally {
                                                                          
                 System.out.println( "i in finally - from try or catch block is:" +i);
                 --i;
                 --i;
                 System.out.println( "i in finally block is:" +i);
                 //return --i;
             }
             return  --i;
}

執行結果:

1
2
3
4
5
6
7
=============WithException3==================
in  try  block  is 10
in  catch  - form  try  block  is 10
in  finally  - from  try  or  catch  block  is 10
in  finally  block  is 8
7
===============================

 

總體結論:

結論一:

(1)return語句並不是函數的最終出口,如果有finally語句,這在return之后還會執行finally(return的值會暫存在棧里面,等待finally執行后再返回)

(2)try、catch、finally語句中,在如果try語句有return語句,則返回的之后當前try中變量此時對應的值,此后對變量做任何的修改,都不影響try中return的返回值

(3)如果finally塊中有return 語句,則返回try或catch中的返回語句忽略。

(4)如果finally塊中拋出異常,則整個try、catch、finally塊中拋出異常


結論二:

   finally里面不建議放return語句,根據需要,return語句可以放在try和catch里面和函數的最后。可行的做法有四:
   (1)return語句只在函數最后出現一次。
   (2)return語句僅在try和catch里面都出現。
   (3)return語句僅在try和函數的最后都出現。
   (4)return語句僅在catch和函數的最后都出現。
   注意,除此之外的其他做法都是不可行的,編譯器會報錯。

注意:

(1)盡量在try或者catch中使用return語句。通過finally塊中達到對try或者catch返回值修改是不可行的。

(2)finally塊中避免使用return語句,因為finally塊中如果使用return語句,會顯示的消化掉try、catch塊中的異常信息,屏蔽了錯誤的發生

(3)finally塊中避免再次拋出異常,否則整個包含try語句塊的方法回拋出異常,並且會消化掉try、catch塊中的異常

 

Reference

[1] http://qing0991.blog.51cto.com/1640542/1387200

[2] https://mp.weixin.qq.com/s?__biz=MzIxNjA5MTM2MA==&mid=2652434506&idx=1&sn=f62a0cc1a18c628e1a52979921184af4&chksm=8c620fc5bb1586d3fe093e03749d9879dbbc4d540f3a68eb18b16d97d973a80a292ba6f7a0c3&mpshare=1&scene=23&srcid=0304GBqHaOULDvnnqLVyCoCk#rd


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM