關於JAVA的try catch finally的return返回值問題
轉載自:《http://blog.csdn.net/chengzhezhijian/article/details/17264531》
下面看一個例子(例1),來講解java裡面中try、catch、finally的處理流程
- publicclass TryCatchFinally {
- @SuppressWarnings("finally")
- publicstaticfinal String test() {
- String t = "";
- try {
- t = "try";
-
return
- } catch (Exception e) {
- // result = "catch";
- t = "catch";
- return t;
- } finally {
- t = "finally";
- }
- }
- publicstaticvoid main(String[] args) {
- System.out.print(TryCatchFinally.test());
- }
-
}
我們用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語句中,在