1. 程式人生 > >try/catch中finally的執行時間

try/catch中finally的執行時間

異常處理:finally塊中程式碼什麼時候執行總結!

前言

由於總是搞不清楚try/catch中的一個執行順序,返回結果。所以總結一下

1.finally沒有return 時,可以看出finally確實在return之前執行了

public static void main(String[] args) {
      int aa = test1();
    System.out.println(aa);
    }


    public  static int  test1(){
        try
{ System.out.println("try"); return 0; }catch(Exception e){ System.out.println("catch"); return 1; }finally { System.out.println("finally"); } } //結果 //try //finally //0

2. finally有return 時,會覆蓋其他語句中的return

public static void main(String[] args) {
      int aa = test1();
    System.out.println(aa);
    }


    public  static int  test1(){
       try{
            System.out.println("try");
            return 0;
        }catch(Exception e){
            System.out.println("catch");
            return 1;
        }finally {
            System.out.println("finally");
           return 2;
        }
    }

    //結果
      //try
    //finally
    //2

3.finally中對基本資料型別沒有影響

 public  static int  test1(){
        int result = 6;
        try{
            System.out.println("try");
            return result;
        }catch(Exception e){
            System.out.println("catch");
            return 1;
        }finally {
            System.out.println("finally");
            result = 3;
        }
    }

    //結果try
    //finally
    //6

4.finally中對引用型資料有影響

public  static StringBuffer  test1(){
       StringBuffer str = new StringBuffer("I");
        try{
            System.out.println("try");
            return str;
        }catch(Exception e){
            System.out.println("catch");
            return null;
        }finally {
            System.out.println("finally");
            str.append("am");
        }
    }

    //結果try
    //finally
    // I am

5.當try/catch外面有異常,finally不執行

 public  static int  test2(){
        int a = 5/0;
        try{
            System.out.println("try");
            return a;
        }catch(Exception e){
            System.out.println("catch");
            return 2;
        }finally {
            System.out.println("finally");
        }
    }

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at com.jxl.face.Controller.EnumTest.test2(EnumTest.java:29)
    at com.jxl.face.Controller.EnumTest.main(EnumTest.java:19)

6.異常在try/catch裡面,finally無return

public  static int  test2(){

        try{
            int a = 5/0;
            System.out.println("try");
            return a;
        }catch(Exception e){
            System.out.println("catch");
            return 2;
        }finally {
            System.out.println("finally");
        }
    }

    //catch
    //finally
    //2

7.異常在try/catch裡面,finally有return

public  static int  test2(){

        try{
            int a = 5/0;
            System.out.println("try");
            return a;
        }catch(Exception e){
            System.out.println("catch");
            return 2;
        }finally {
            System.out.println("finally");
            return 3;
        }
    }

    //catch
    //finally
    //3