關於Java中try-catch-finally-return的執行順序

檻外之人發表於2016-06-21

原文地址:http://qing0991.blog.51cto.com/1640542/1387200

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 is10
in finally - from try or catch block is9
8
===============================


執行順序:

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

結論:try-catch-finally都有return語句時,沒有異常時,返回值是finally中的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 is10
in finally - from try or catch block is9
in finally block is8
9
===============================

執行順序:

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

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


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 is10
in catch - form try block is10
in catch block is9
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 is10
in catch - form try block is10
in finally - from try or catch block is9
in finally block is8
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 is10
in catch - form try block is10
in finally - from try or catch block is10
in finally block is8
7
===============================


總體結論:

結論一:

   return語句並不是函式的最終出口,如果有finally語句,這在return之後還會執行finally(return的值會暫存在棧裡面,等待finally執行後再返回)
結論二:

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



相關文章