Java中關於try、catch、finally中的細節分析

本文講解的是關於Java中關於try、catch、finally中一些問題

下面看一個例子(例1),來講解java裏面中try、catch、finally的處理流程

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";
       }
   }

   public static void main(String[] args) {
       System.out.print(TryCatchFinally.test());
   }

}

 首先程序執行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塊中的異常

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章