try、catch、finally、return的執行順序

來源:互聯網
上載者:User

http://blog.csdn.net/neusoftware_20063500/article/details/4347865

 

public class JVMTest {

public static void main(String[] args){
System.out.println("aa:" + aa());
}
public static int aa(){
int a = 1;
int b = 10;
try{
System.out.println("abc");
return a;
}finally{
a = 2;
System.out.println("a: "+ a);
}
}
}

運行結果為:

abc
a: 2
aa:1

由此可知:在try語句中,在執行return語句時,要返回的結果已經準備好了,就在此時,程式轉到finally執行了。

在轉去之前,try中先把要返回的結果存放到不同於a的局部變數中去,執行完finally之後,在從中取出返回結果,

因此,即使finally中對變數a進行了改變,但是不會影響返回結果。

但是,如果在finally子句中最後添加上return a會怎樣呢?

執行結果如下:

Compiling 1 source file to E:/sun/InsideJVM/build/classes
E:/sun/InsideJVM/src/JVMTest.java:37: warning: finally clause cannot complete normally

}
1 warning
compile-single:
run-single:
abc
a: 2
aa:2 

 

測試1:
    public static int test1()
    {
        int i = 1;
        try
        {
            return ++i;
        }
        finally
        {
            ++i;
            Console.WriteLine("finally:" + i);
        }
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Main:" + test1());
    }
結果:
finally:3
Main:2

 

測試2:
    public static int test2()
    {
        int i = 1;
        try
        {
            throw new Exception();
        }
        catch
        {
            return ++i;
        }
        finally
        {
            ++i;
            Console.WriteLine("finally:" + i);
        }
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Main:" + test2());
    }
結果:
finally:3
Main:2

 

測試3:
    public static int test3()
    {
        try{}
        finally
        {
            return 1;
        }
    }

結果:
編譯錯誤,控制不能離開 finally 子句主體。

 

結論:

1.不管出沒出現異常,finally塊中的語句都會執行;
2.當try或catch塊中有return語句時,finally塊中的語句仍會執行;
3.finally塊中的語句是在return語句執行之後才執行的,即函數傳回值是在finally塊中語句執行前確定的;
4.finally塊中不能包含return語句。

 

 

總結:finally在return前執行,在finally的操作,不會改變已經確定的return的值,

finally不能加return語句。出現異常,先找是否有處理器可以處理這個異常.再finally。

 

public class JVMTest {

public static void main(String[] args){
System.out.println("aa:" + aa());
}
public static int aa(){
int a = 1;
int b = 10;
try{
System.out.println("abc");
return a;
}finally{
a = 2;
System.out.println("a: "+ a);
}
}
}

運行結果為:

abc
a: 2
aa:1

由此可知:在try語句中,在執行return語句時,要返回的結果已經準備好了,就在此時,程式轉到finally執行了。

在轉去之前,try中先把要返回的結果存放到不同於a的局部變數中去,執行完finally之後,在從中取出返回結果,

因此,即使finally中對變數a進行了改變,但是不會影響返回結果。

但是,如果在finally子句中最後添加上return a會怎樣呢?

執行結果如下:

Compiling 1 source file to E:/sun/InsideJVM/build/classes
E:/sun/InsideJVM/src/JVMTest.java:37: warning: finally clause cannot complete normally

}
1 warning
compile-single:
run-single:
abc
a: 2
aa:2 

 

測試1:
    public static int test1()
    {
        int i = 1;
        try
        {
            return ++i;
        }
        finally
        {
            ++i;
            Console.WriteLine("finally:" + i);
        }
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Main:" + test1());
    }
結果:
finally:3
Main:2

 

測試2:
    public static int test2()
    {
        int i = 1;
        try
        {
            throw new Exception();
        }
        catch
        {
            return ++i;
        }
        finally
        {
            ++i;
            Console.WriteLine("finally:" + i);
        }
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Main:" + test2());
    }
結果:
finally:3
Main:2

 

測試3:
    public static int test3()
    {
        try{}
        finally
        {
            return 1;
        }
    }

結果:
編譯錯誤,控制不能離開 finally 子句主體。

 

結論:

1.不管出沒出現異常,finally塊中的語句都會執行;
2.當try或catch塊中有return語句時,finally塊中的語句仍會執行;
3.finally塊中的語句是在return語句執行之後才執行的,即函數傳回值是在finally塊中語句執行前確定的;
4.finally塊中不能包含return語句。

 

 

總結:finally在return前執行,在finally的操作,不會改變已經確定的return的值,

finally不能加return語句。出現異常,先找是否有處理器可以處理這個異常.再finally。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.