天天看點

JAVA随時筆記(一)try,catch,finally的傳回值總結

1、

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。

編譯出來的位元組碼部分資訊,我們隻看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、

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

先進行try{}語句,然後在return之前把目前的t的值try儲存到一個變量t',然後執行finally語句塊,修改了變量t的值,在傳回變量t。傳回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塊中抛出異常

注意:

1 盡量在try或者catch中使用return語句。通過finally塊中達到對try或者catch傳回值修改是不可行的。

2 finally塊中避免使用return語句,因為finally塊中如果使用return語句,會顯示的消化掉try、catch塊中的異常資訊,屏蔽了錯誤的發生

3 finally塊中避免再次抛出異常,否則整個包含try語句塊的方法回抛出異常,并且會消化掉try、catch塊中的異常。