轉自:https://blog.csdn.net/S_gy_Zetrov/article/details/68490882、
Java中關于try…catch…finally異常處理的細節(jié)辨析
什么時候執(zhí)行finally; finally后面的語句執(zhí)行嗎; try…catch…finally塊中的finally語句是不是一定會被執(zhí)行; Java中finally與return的執(zhí)行順序詳解;
首先需要明確幾點:
try語句塊中的代碼應是可能出現(xiàn)異常的代碼,可能會拋出一個或多個異常,因此,try后面可跟一個或多個catch
如果異常間關系不大,則catch的順序可以隨意。但如果異常間有父類子類繼承關系,則必須將子類異常catch放置在父類異常catch前面,以防止一場直接被父類catch接住,沒有輸出我們想要的具體子類異常信息
如果try中沒有出現(xiàn)異常,則catch不被執(zhí)行。但不管catch接沒接住,try有沒有異常,只要有finally,則都會執(zhí)行finally(general情況,其他情況詳見后)。
finally后面的語句是否執(zhí)行與try或catch中有無return有關
這篇博客耗的時間比較長,我仔細驗證了幾個猜測?,F(xiàn)在記錄下來。
整篇文章較長,閱讀大約需要8min
finally后面的語句什么時候執(zhí)行?
還是先看一個例子:
public class TestFinally {
public static void main(String[] args){
try{
return;
}
finally{
System.out.println("Finally");
}
}
}
輸出:
Finally
可以看出:當try中無異常發(fā)生,finally仍執(zhí)行
如果去除return更換為其它普通語句
public class TestFinally {
public static void main(String[] args){
try{
int a=3;
a+=5;
System.out.println(a);
}
finally{
System.out.println("Finally");
}
int b=5;
b+=5;
System.out.println(b);
}
}
輸出:
Finally
可以看出:finally后面語句正常執(zhí)行
如果try中加入return,不能編譯,提示
int b=5;
b+=5;
System.out.println(b);
這部分代碼是unreachable code
得證如果try中有return,則finally后面的代碼不會執(zhí)行(for ‘catch’ is the same thing)
附上一些證明栗子,可以跳過
例1
public class TestFinally {
public static void a() throws Exception{
try{
throw new Exception();
}
catch(Exception e){
System.out.println("exception000");
}
finally{
System.out.println("finally111");
}
}
public static void main(String[] args){
try{
a();
}
catch(Exception e){
System.out.println("exception");
}
System.out.println("finished");
}
}
輸出:
exception000
finally111
finished
異常在方法中被接住,main中catch不會執(zhí)行,catch后的代碼正常執(zhí)行
刪去exception000項后
public class TestFinally {
public static void a() throws Exception{
try{
throw new Exception();
}
/catch(Exception e){
System.out.println("exception000");
}/
finally{
System.out.println("finally111");
}
}
public static void main(String[] args){
try{
a();
}
catch(Exception e){
System.out.println("exception");
}
System.out.println("finished");
}
}
輸出:
finally111
exception
finished
異常方法中沒被接住,但在main中被接住,catch后代碼正常執(zhí)行
刪去finally111項后
package trycatchfinally;
public class TestFinally {
public static void a() throws Exception{
try{
throw new Exception();
}
catch(Exception e){
System.out.println("exception000");
}
/finally{
System.out.println("finally111");
}/
}
public static void main(String[] args){
try{
a();
}
catch(Exception e){
System.out.println("exception");
}
System.out.println("finished");
}
}
輸出:
exception000
finished
finally被注釋,故不執(zhí)行,異常被方法中的catch接住,main中catch后代碼正常執(zhí)行
例2
public class TestFinally {
public static String output="";
public static void a(int i){
try{
if(i==1){
throw new Exception();
}
output+="1";
}
catch(Exception e){
output+="2";
return;
}
finally{output+="3";}
output+="4";
}
public static void main(String[] args){
a(0);
a(1);
System.out.println(TestFinally.output);
}
}
輸出:
13423
可以看出,catch中有return,則finally后面的代碼不會執(zhí)行
去掉return后
package trycatchfinally;
public class TestFinally {
public static String output="";
public static void a(int i){
try{
if(i==1){
throw new Exception();
}
output+="1";
}
catch(Exception e){
output+="2";
//return;
}
finally{output+="3";}
output+="4";
}
public static void main(String[] args){
a(0);
a(1);
System.out.println(TestFinally.output);
}
}
輸出:
1234234
finally后代碼執(zhí)行,得證。
什么時候finally執(zhí)行?什么時候不執(zhí)行?
經(jīng)過搜索,找到一位前輩的博客,她寫到至少兩種情況下finally是出現(xiàn)但不執(zhí)行的
try語句沒有被執(zhí)行到,如在try語句之前就返回了,這樣finally語句就不會執(zhí)行,這也說明了finally語句被執(zhí)行的必要而非充分條件是:相應的try語句一定被執(zhí)行到。
在try塊中有System.exit(0);這樣的語句,System.exit(0);是終止Java虛擬機JVM的,連JVM都停止了,所有都結束了,當然finally語句也不會被執(zhí)行到。
接下來的內容就比較深入細節(jié)了,沒有這方面了解的需求可直接略過
現(xiàn)在我們已經(jīng)知道finally后面的語句執(zhí)行與try/catch中有無return有關,但finally塊本身執(zhí)行是在return的前面還是后面還是什么時候?
一個我比較認同的結論:
finally塊的語句在try或catch中的return語句執(zhí)行之后返回之前執(zhí)行且finally里的修改語句可能影響也可能不影響try或catch中 return已經(jīng)確定的返回值,若finally里也有return語句則覆蓋try或catch中的return語句直接返回
要想明白這個問題,需要一些背景知識:
java方法是在棧幀中執(zhí)行,棧幀是線程私有棧的單位,執(zhí)行方法的線程會為每一個方法分配一小塊??臻g來作為該方法執(zhí)行時的內存空間,棧幀分為三個區(qū)域:
操作數(shù)棧,用來保存正在執(zhí)行的表達式中的操作數(shù),數(shù)據(jù)結構中學習過基于棧的多項式求值算法,操作數(shù)棧的作用和這個一樣
局部變量區(qū),用來保存方法中使用的變量,包括方法參數(shù),方法內部聲明的變量,以及方法中使用到的對象的成員變量或類的成員變量(靜態(tài)變量),最后兩種變量會復制到局部變量區(qū),因此在多線程 環(huán)境下,這種變量需要根據(jù)需要聲明為volatile類型
字節(jié)碼指令區(qū),這個不用解釋了,就是方法中的代碼翻譯成的指令
return語句:
return語句的格式如下:
return [expression];
其中expression(表達式)是可選的,因為有些方法沒有返回值,所以return后面也就沒有表達式,或者可以看做是空的表達式。
我們知道return語句的作用可以結束方法并返回一個值,那么他返回的是哪里的值呢?返回的是return指令執(zhí)行的時刻,操作數(shù)棧頂?shù)闹?,不管expression是一個怎樣的表達式,究竟做了些什么工作,對于return指令來說都不重要,他只負責把操作數(shù)棧頂?shù)闹捣祷亍?/p>
而return expression是分成兩部分執(zhí)行的:
- 執(zhí)行:expression;
- 執(zhí)行:return指令;
例如:return x+y;
這句代碼先執(zhí)行x+y,再執(zhí)行return;首先執(zhí)行將x以及y從局部變量區(qū)復制到操作數(shù)棧頂?shù)闹噶?,然后?zhí)行加法指令,這個時候結果x+y的值會保存在操作數(shù)棧的棧頂,最后執(zhí)行return指令,返回操作數(shù)棧頂?shù)闹怠?/p>
對于return x;先執(zhí)行x,x也是一個表達式,這個表達式只有一個操作數(shù),會執(zhí)行將變量x從局部變量區(qū)復制到操作數(shù)棧頂?shù)闹噶?,然后?zhí)行return,返回操作數(shù)棧頂?shù)闹怠?/p>
因此return x實際返回的是return指令執(zhí)行時,x在操作數(shù)棧頂?shù)囊粋€快照或者叫副本,而不是x這個值。
finally語句在return語句執(zhí)行之后還是之前執(zhí)行?
栗子1:
public class TestFinally {
public static void main(String[] args) {
System.out.println(test1());
}
public static int test1() {
int b = 20;
try {
System.out.println("try");
return b += 80;
}
catch (Exception e) {
System.out.println("catch");
}
finally {
System.out.println("finally");
if (b > 25) {
System.out.println("b>25, b = " + b);
}
}
return b;
}
}
輸出:
try
finally
b>25, b = 100
100
說明return語句已經(jīng)執(zhí)行了再去執(zhí)行finally語句,不過并沒有直接返回,而是等finally語句執(zhí)行完了再返回結果。即:finally語句在return語句執(zhí)行之后return返回之前執(zhí)行的。
再來一個栗子2:
public class TestFinally {
public static void main(String[] args) {
System.out.println(test11());
}
public static String test11() {
try {
System.out.println("try");
return test12();
} finally {
System.out.println("finally");
}
}
public static String test12() {
System.out.println("return statement");
return "after return";
}
}
輸出:
try
return statement
finally
after return
如果finally里也有return語句,那么是不是就直接返回了,try中的return就不能返回了?
public class TestFinally {
public static void main(String[] args) {
System.out.println(test2());
}
public static int test2() {
int b = 20;
try {
System.out.println("try");
return b += 80;
} catch (Exception e) {
System.out.println("catch");
} finally {
System.out.println("finally");
if (b > 25) {
System.out.println("b>25, b = " + b);
}
return 200;
}
// return b;
}
}
輸出:
try
finally
b>25, b = 100
200
說明finally里的return直接返回了,就不管try中是否還有返回語句,這里還有個小細節(jié)需要注意,finally里加上return過后,finally外面的return b就變成不可到達語句了,也就是永遠不能被執(zhí)行到,所以需要注釋掉否則編譯器報錯。即finally塊中的return語句會覆蓋try塊中的return返回。
如果finally中沒有return,會如何返回?
try{
return expression;
}finally{
do some work;
}
首先我們知道,finally語句是一定會執(zhí)行,但他們的執(zhí)行順序是怎么樣的呢?他們的執(zhí)行順序如下:
1.執(zhí)行:expression,計算該表達式,結果保存在操作數(shù)棧頂;
2.執(zhí)行:操作數(shù)棧頂值(expression的結果)復制到局部變量區(qū)作為返回值;
3.執(zhí)行:finally語句塊中的代碼;
4.執(zhí)行:將第2步復制到局部變量區(qū)的返回值又復制回操作數(shù)棧頂;
5.執(zhí)行:return指令,返回操作數(shù)棧頂?shù)闹担?/p>
我們可以看到,在第一步執(zhí)行完畢后,整個方法的返回值就已經(jīng)確定了,由于還要執(zhí)行finally代碼塊,因此程序會將返回值暫存在局部變量區(qū),騰出操作數(shù)棧用來執(zhí)行finally語句塊中代碼,等finally執(zhí)行完畢,再將暫存的返回值又復制回操作數(shù)棧頂。所以無論finally語句塊中執(zhí)行了什么操作,都無法影響返回值,所以試圖在finally語句塊中修改返回值是徒勞的。因此,finally語句塊設計出來的目的只是為了讓方法執(zhí)行一些重要的收尾工作,而不是用來計算返回值的。
但我前面剛說finally里的return直接返回了,就不管try中是否還有返回語句,都不管try直接返回了,這是不是矛盾呢?
并不是。仔細閱讀剛才的代碼,可以看到,執(zhí)行return后面的表達式,但值并沒有立即返回,會轉去執(zhí)行finally,然后再返回剛才沒有返回的值。如果finally里面返回了一個常值如return 200,這時才會直接返回,不管之前要返回的值。
如果finally里沒有return語句,但修改了b的值,那么try中return返回的是修改后的值還是原值?
public class TestFinally {
public static void main(String[] args) {
System.out.println(test3());
}
public static int test3() {
int b = 20;
try {
System.out.println("try");
return b += 80;
} catch (Exception e) {
System.out.println("catch");
} finally {
System.out.println("finally");
if (b > 25) {
System.out.println("b>25, b = " + b);
}
b = 150;
}
return 2000;
}
}
輸出:
try
finally
b>25, b = 100
100
如果return使用在基本數(shù)據(jù)變量上,則finally中對改基本數(shù)據(jù)變量的修改不會生效!如果作用的是對象如Integer包裝類或者其他正常對象如`Map
每次返回的一定是try中的return語句?那么finally后面的return語句(如果有)永遠不會執(zhí)行?
try塊里的return語句在異常的情況下不會被執(zhí)行,這樣具體返回哪個看情況
public class TestFinally {
public static void main(String[] args) {
System.out.println(test4());
}
public static int test4() {
int b = 20;
try {
System.out.println("try");
b = b / 0;
return b += 80;
} catch (Exception e) {
b += 15;
System.out.println("catch");
} finally {
System.out.println("finally");
if (b > 25) {
System.out.println("b>25, b = " + b);
}
b += 50;
}
return 26;
}
}
輸出:
try
catch
finally
b>25, b = 35
26
這里因為在return之前發(fā)生了除0異常,所以try中的return不會被執(zhí)行到,而是接著執(zhí)行捕獲異常的catch 語句和最終的finally語句,此時兩者對b的修改都影響了最終的返回值,這時return b;就起到作用了。當然如果將return b改為return 300什么的,最后返回的就是300。
如果catch中有return,則執(zhí)行情況與未發(fā)生異常時try中return的執(zhí)行情況完全一樣。
public class TestFinally {
public static void main(String[] args) {
System.out.println(test5());
}
public static int test5() {
int b = 20;
try {
System.out.println("try");
b = b /0;
return b += 80;
} catch (Exception e) {
System.out.println("catch");
return b += 15;
} finally {
System.out.println("finally");
if (b > 25) {
System.out.println("b>25, b = " + b);
}
b += 50;
}
//return b;
//Unreachable code
}
}
輸出:
try
catch
finally
b>25, b = 35
35
說明了發(fā)生異常后,catch中的return語句先執(zhí)行,確定了返回值后再去執(zhí)行finally塊,執(zhí)行完了catch再返回,finally里對b的改變對返回值無影響,原因同前面一樣,也就是說情況與try中的return語句執(zhí)行完全一樣。
如果想輸出85,需要這么改,即finally中return,覆蓋catch中的try。
public class TestFinally {
public static void main(String[] args) {
System.out.println(test5());
}
public static int test5() {
int b = 20;
try {
System.out.println("try block");
b = b /0;
return b += 80;
} catch (Exception e) {
System.out.println("catch block");
return b += 15;
} finally {
System.out.println("finally block");
if (b > 25) {
System.out.println("b>25, b = " + b);
}
b += 50;
return b;
}
//return b;
}
}
輸出:
try block
catch block
finally block
b>25, b = 35
85
至此,此篇結束。關于這塊的問題相信我已經(jīng)明白了二三事,希望看到這的你也有收獲 :)
參考文獻:
1:http://www.cnblogs.com/lanxuezaipiao/p/3440471.html
2:http://blog.csdn.net/qj19842011/article/details/45675057