下面看一個例子(例1),來講解java裡面中try、catch、finally的處理流程
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
首先程式執行try語句塊,把變量t指派為try,由于沒有發現異常,接下來執行finally語句塊,把變量t指派為finally,然後return t,則t的值是finally,最後t的值就是finally,程式結果應該顯示finally,但是實際結果為try。為什麼會這樣,我們不妨先看看這段代碼編譯出來的class對應的位元組碼,看虛拟機内部是如何執行的。
我們用javap -verbose TryCatchFinally 來顯示目标檔案(.class檔案)位元組碼資訊
系統運作環境:mac os lion系統 64bit
jdk資訊:Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-11M3527) Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02-402, mixed mode)
編譯出來的位元組碼部分資訊,我們隻看test方法,其他的先忽略掉

public static final java.lang.String test();
Code:
Stack=1, Locals=4, Args_size=0
0: ldc #16; //String
2: astore_0
3: ldc #18; //String try
5: astore_0
6: aload_0
7: astore_3
8: ldc #20; //String finally
10: astore_0
11: aload_3
12: areturn
13: astore_1
14: ldc #22; //String catch
16: astore_0
17: aload_0
18: astore_3
19: ldc #20; //String finally
21: astore_0
22: aload_3
23: areturn
24: astore_2
25: ldc #20; //String finally
27: astore_0
28: aload_2
29: athrow
Exception table:
from to target type
3 8 13 Class java/lang/Exception
3 8 24 any
13 19 24 any
LineNumberTable:
line 5: 0
line 8: 3
line 9: 6
line 15: 8
line 9: 11
line 10: 13
line 12: 14
line 13: 17
line 15: 19
line 13: 22
line 14: 24
line 15: 25
line 16: 28
LocalVariableTable:
Start Length Slot Name Signature
3 27 0 t Ljava/lang/String;
14 10 1 e Ljava/lang/Exception;
StackMapTable: number_of_entries = 2
frame_type = 255 /* full_frame */
offset_delta = 13
locals = [ class java/lang/String ]
stack = [ class java/lang/Exception ]
frame_type = 74 /* same_locals_1_stack_item */
stack = [ class java/lang/Throwable ]

首先看LocalVariableTable資訊,這裡面定義了兩個變量 一個是t String類型,一個是e Exception 類型
接下來看Code部分
第[0-2]行,給第0個變量指派“”,也就是String t="";
第[3-6]行,也就是執行try語句塊 指派語句 ,也就是 t = "try";
第7行,重點是第7行,把第s對應的值"try"付給第三個變量,但是這裡面第三個變量并沒有定義,這個比較奇怪
第[8-10] 行,對第0個變量進行指派操作,也就是t="finally"
第[11-12]行,把第三個變量對應的值傳回
通過位元組碼,我們發現,在try語句的return塊中,return 傳回的引用變量(t 是引用類型)并不是try語句外定義的引用變量t,而是系統重新定義了一個局部引用t’,這個引用指向了引用t對應的值,也就是try ,即使在finally語句中把引用t指向了值finally,因為return的傳回引用已經不是t ,是以引用t的對應的值和try語句中的傳回值無關了。
下面在看一個例子:(例2)

1 public class TryCatchFinally {
2
3 @SuppressWarnings("finally")
4 public static final String test() {
5 String t = "";
6
7 try {
8 t = "try";
9 return t;
10 } catch (Exception e) {
11 // result = "catch";
12 t = "catch";
13 return t;
14 } finally {
15 t = "finally";
16 return t;
17 }
18 }
19
20 public static void main(String[] args) {
21 System.out.print(TryCatchFinally.test());
22 }
23
24 }

這裡稍微修改了 第一段代碼,隻是在finally語句塊裡面加入了 一個 return t 的表達式。
按照第一段代碼的解釋,先進行try{}語句,然後在return之前把目前的t的值try儲存到一個變量t',然後執行finally語句塊,修改了變量t的值,在傳回變量t。
這裡面有兩個return語句,但是程式到底傳回的是try 還是 finally。接下來我們還是看位元組碼資訊

public static final java.lang.String test();
Code:
Stack=1, Locals=2, Args_size=0
0: ldc #16; //String
2: astore_0
3: ldc #18; //String try
5: astore_0
6: goto 17
9: astore_1
10: ldc #20; //String catch
12: astore_0
13: goto 17
16: pop
17: ldc #22; //String finally
19: astore_0
20: aload_0
21: areturn
Exception table:
from to target type
3 9 9 Class java/lang/Exception
3 16 16 any
LineNumberTable:
line 5: 0
line 8: 3
line 9: 6
line 10: 9
line 12: 10
line 13: 13
line 14: 16
line 15: 17
line 16: 20
LocalVariableTable:
Start Length Slot Name Signature
3 19 0 t Ljava/lang/String;
10 6 1 e Ljava/lang/Exception;
StackMapTable: number_of_entries = 3
frame_type = 255 /* full_frame */
offset_delta = 9
locals = [ class java/lang/String ]
stack = [ class java/lang/Exception ]
frame_type = 70 /* same_locals_1_stack_item */
stack = [ class java/lang/Throwable ]
frame_type = 0 /* same */

這段代碼翻譯出來的位元組碼和第一段代碼完全不同,還是繼續看code屬性
第[0-2]行、[3-5]行第一段代碼邏輯類似,就是初始化t,把try中的t進行指派try
第6行,這裡面跳轉到第17行,[17-19]就是執行finally裡面的指派語句,把變量t指派為finally,然後傳回t對應的值
我們發現try語句中的return語句給忽略。可能jvm認為一個方法裡面有兩個return語句并沒有太大的意義,是以try中的return語句給忽略了,直接起作用的是finally中的return語句,是以這次傳回的是finally。
接下來在看看複雜一點的例子:(例3)

public class TryCatchFinally {
@SuppressWarnings("finally")
public static final String test() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (Exception e) {
t = "catch";
return t;
} finally {
t = "finally";
// System.out.println(t);
// return t;
}
}
public static void main(String[] args) {
System.out.print(TryCatchFinally.test());
}
}

這裡面try語句裡面會抛出 java.lang.NumberFormatException,是以程式會先執行catch語句中的邏輯,t指派為catch,在執行return之前,會把傳回值儲存到一個臨時變量裡面t ',執行finally的邏輯,t指派為finally,但是傳回值和t',是以變量t的值和傳回值已經沒有關系了,傳回的是catch
例4:

public class TryCatchFinally {
@SuppressWarnings("finally")
public static final String test() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (Exception e) {
t = "catch";
return t;
} finally {
t = "finally";
return t;
}
}
public static void main(String[] args) {
System.out.print(TryCatchFinally.test());
}
}

這個和例2有點類似,由于try語句裡面抛出異常,程式轉入catch語句塊,catch語句在執行return語句之前執行finally,而finally語句有return,則直接執行finally的語句值,傳回finally
例5:

public class TryCatchFinally {
@SuppressWarnings("finally")
public static final String test() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (Exception e) {
t = "catch";
Integer.parseInt(null);
return t;
} finally {
t = "finally";
//return t;
}
}
public static void main(String[] args) {
System.out.print(TryCatchFinally.test());
}
}

這個例子在catch語句塊添加了Integer.parser(null)語句,強制抛出了一個異常。然後finally語句塊裡面沒有return語句。繼續分析一下,由于try語句抛出異常,程式進入catch語句塊,catch語句塊又抛出一個異常,說明catch語句要退出,則執行finally語句塊,對t進行指派。然後catch語句塊裡面抛出異常。結果是抛出java.lang.NumberFormatException異常
例子6:

public class TryCatchFinally {
@SuppressWarnings("finally")
public static final String test() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (Exception e) {
t = "catch";
Integer.parseInt(null);
return t;
} finally {
t = "finally";
return t;
}
}
public static void main(String[] args) {
System.out.print(TryCatchFinally.test());
}
}

這個例子和上面例子中唯一不同的是,這個例子裡面finally 語句裡面有return語句塊。try catch中運作的邏輯和上面例子一樣,當catch語句塊裡面抛出異常之後,進入finally語句快,然後傳回t。則程式忽略catch語句塊裡面抛出的異常資訊,直接傳回t對應的值 也就是finally。方法不會抛出異常
例子7:

public class TryCatchFinally {
@SuppressWarnings("finally")
public static final String test() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (NullPointerException e) {
t = "catch";
return t;
} finally {
t = "finally";
}
}
public static void main(String[] args) {
System.out.print(TryCatchFinally.test());
}
}

這個例子裡面catch語句裡面catch的是NPE異常,而不是java.lang.NumberFormatException異常,是以不會進入catch語句塊,直接進入finally語句塊,finally對s指派之後,由try語句抛出java.lang.NumberFormatException異常。
例子8

public class TryCatchFinally {
@SuppressWarnings("finally")
public static final String test() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (NullPointerException e) {
t = "catch";
return t;
} finally {
t = "finally";
return t;
}
}
public static void main(String[] args) {
System.out.print(TryCatchFinally.test());
}
}

和上面的例子中try catch的邏輯相同,try語句執行完成執行finally語句,finally指派s 并且傳回s ,最後程式結果傳回finally
例子9:

public class TryCatchFinally {
@SuppressWarnings("finally")
public static final String test() {
String t = "";
try {
t = "try";return t;
} catch (Exception e) {
t = "catch";
return t;
} finally {
t = "finally";
String.valueOf(null);
return t;
}
}
public static void main(String[] args) {
System.out.print(TryCatchFinally.test());
}
}

這個例子中,對finally語句中添加了String.valueOf(null), 強制抛出NPE異常。首先程式執行try語句,在傳回執行,執行finally語句塊,finally語句抛出NPE異常,整個結果傳回NPE異常。
對以上所有的例子進行總結
1 try、catch、finally語句中,在如果try語句有return語句,則傳回的之後目前try中變量此時對應的值,此後對變量做任何的修改,都不影響try中return的傳回值
2 如果finally塊中有return 語句,則傳回try或catch中的傳回語句忽略。
3 如果finally塊中抛出異常,則整個try、catch、finally塊中抛出異常
是以使用try、catch、finally語句塊中需要注意的是
1 盡量在try或者catch中使用return語句。通過finally塊中達到對try或者catch傳回值修改是不可行的。
2 finally塊中避免使用return語句,因為finally塊中如果使用return語句,會顯示的消化掉try、catch塊中的異常資訊,屏蔽了錯誤的發生
3 finally塊中避免再次抛出異常,否則整個包含try語句塊的方法回抛出異常,并且會消化掉try、catch塊中的異常
本文轉載自http://www.cnblogs.com/aigongsi/