try {}里有一個return語句,那么緊跟在這個try后的finally {}里的code會不會被執行,什么時候被執行,在return前還是后?


一, 

package demo;

public class tryDemo {
    private static int see(){
        int s = 0;
        try{
            s +=2;
            System.out.println("執行完try");
            return s;
        }catch(Exception e){
            return s+1;
        }finally{
            System.out.println("執行完finally");
            
        }
    }
    public static void main(String[] args) {
        int a = see();
        System.out.println("a : "+ a );
    }
}

運行結果:

執行完try
執行完finally
a : 2

說明,如果 try當中有return 的話, 先執行完return中的語句,在執行finally中的語句,最后 返回 try 中的 return。

二,如果 finally當中也有 return呢 ?

package demo;

public class tryDemo {
    private static int see(){
        int s = 0;
        try{
            s +=2;
            System.out.println("執行完try");
            return s;
        }catch(Exception e){
            return s+1;
        }finally{
            System.out.println("執行完finally");
            return s+3;
            
        }
    }
    public static void main(String[] args) {
        int a = see();
        System.out.println("a : "+ a );
    }
}

輸出結果:

執行完try
執行完finally
a : 5

說明: 這種情況下,不執行 try當中的 return ,最后返回的是 finally 中的值 。

三 ,

如果 try當中有 return s,

finally沒有 return, 但在 finally 中 有 對 s 的操作,那么 try 當中的 return  s 是 更改后的 s 嗎?

package demo;

public class tryDemo {
    private static int see(){
        int s = 0;
        try{
            s +=2;
            System.out.println("執行完try");
            return s;
        }catch(Exception e){
            return s+1;
        }finally{
            System.out.println("執行完finally");
            s +=1;
            //s=5;
            
        }
    }
    public static void main(String[] args) {
        int a = see();
        System.out.println("a : "+ a );
    }
}

輸出結果:

執行完try
執行完finally
a : 2

說明, finally當中的 對 s 的修改 並不對 try當中的 return 值產生影響。

四,針對 int類型 和 List 類型的一個小區別

1,針對 int 類型

package tuling;

import java.util.ArrayList;
import java.util.List;

public class ErrorAndThr0 {
        public static void main(String[] args) {
            int a = test();
            System.out.println("a = "+ a);
        }
        
        private static int test(){

            int i = 0;
            try{
                i++;
                System.out.println("try block,i = " + i);
                return i;
            }catch(Exception e){
                i++;
                System.out.println("catch block , i = "+ i );
                return i;
            }finally{
                i++;
                System.out.println("finally block, i = "+ i);
            }
        
        }

    


}

輸出結果:

try block,i = 1
finally block, i = 2
a = 1

我們看下.class文件反編譯的源代碼:

 

 當然 ,紅色 的部分 還未得到證實, 我是看  03-Exception和Error的區別-楊過 的 第 16分鍾 是這么說的 。 

 

2, 針對 List 類型

package tuling;

import java.util.ArrayList;
import java.util.List;

public class ErrorAndThr {
    public static void main(String[] args) {
        List <String> list = test();
        System.out.println("list : "+ list.toString());
    }
    
    private static List test(){

        List <String> list = new ArrayList<String>();
        try{
            list.add("step try");
            System.out.println("try block");
            return list;
        }catch(Exception e){
            list.add("step catch");
            System.out.println("catch block");
            return list;
        }finally{
            list.add("step finally");
            System.out.println("finally block");
        }
    
    }

}

輸出結果:

try block
finally block
list : [step try, step finally]

我們看下.class文件反編譯的源代碼:

 

 說明,針對 list類型,finally類型的操作會影響 try 當中return的結果。

 


免責聲明!

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



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