隨筆-204  評論-149  文章-0  trackbacks-0

          由一個筆試題想到的
          try有return,catch也有return,且有finally塊
          并且finally中對resutl進行的賦值并不影響之前的已經return 的result值
          最后返回的result引用還是之前的result引用
          finally其實也可以有return,一般eclipse會提示finally block does not complete normally,但不會報錯
          并且會忽略之前的返回和拋出的異常

          package returnfinally;

          public class TestReturnAfterFinally {

               
          public static void main(String[] args) {
                
          try {
                 String result 
          = testException();
                 System.out.println();
                 System.out.println(result);
                }
           catch (Exception e) {
                
          // System.out.println(result);
                 System.out.println(e.toString());
                }

               }

               
              
          public static String testException() throws Exception {
                String result 
          = "";
                
          try {
                 
          // if (true) throw new MyException("異常!");
                 
          //int i = 0;
                 int i=0;
                 
          int b = 10 / i;
                 
          //函數不會正常的返回,在返回前已經拋出異常了,拋出異常即函數返回了
                 /*
                  * 相當于catch(Exception e){
                  *    throw e;
                  * }
                  
          */

                 
          /*
                 result="in try";
                 return result;
                 
          */

                 
          throw new Exception("h");
                }
          catch(Exception e){
                    System.out.println(e);
                    Exception ee 
          = e;
                    
          //throw ee;
                    result="in catch";
                    
          return result;
                }

                
          finally {
                 result 
          = "in finally!";
                 System.out.println(result);
                 
          //return result;
                 
          //throw new Exception("ex cretated in finally");
                }

                
                
          //return result;
               }


              }




           

          posted on 2009-04-30 16:04 Frank_Fang 閱讀(2277) 評論(3)  編輯  收藏 所屬分類: Java編程

          評論:
          # re: java try return finally 2009-04-30 16:17 | Frank_Fang
          剛看見一個分析,不過后面的使用javap沒看懂
          http://blog.csdn.net/nomad2/archive/2006/05/10/722764.aspx  回復  更多評論
            
          # re: java try return finally 2009-05-24 23:14 | Frank_Fang
          主要關注一下finally中有return 或者throw時程序的執行流程
            回復  更多評論
            
          # re: java try return finally 2009-05-24 23:14 | Frank_Fang
          java中finally子句是怎么運行的?

          看下面的程序:

          public class JVMTest {

          public static void main(String[] args){
          System.out.println("aa:" + aa());
          }
          public static int aa(){
          int a = 1;
          int b = 10;
          try{
          System.out.println("abc");
          return a;
          }finally{
          a = 2;
          System.out.println("a: "+ a);
          }
          }
          }

          運行結果為:

          abc
          a: 2
          aa:1

          由此可知:在try語句中,在執行return語句時,要返回的結果已經準備好了,就在此時,程序轉到finally執行了。

          在轉去之前,try中先把要返回的結果存放到不同于a的局部變量中去,執行完finally之后,在從中取出返回結果,

          因此,即使finally中對變量a進行了改變,但是不會影響返回結果。

          但是,如果在finally子句中最后添加上return a會怎樣呢?

          執行結果如下:

          Compiling 1 source file to E:\sun\InsideJVM\build\classes
          E:\sun\InsideJVM\src\JVMTest.java:37: warning: finally clause cannot complete normally
          }
          1 warning
          compile-single:
          run-single:
          abc
          a: 2
          aa:2

            回復  更多評論
            
          主站蜘蛛池模板: 涞水县| 泰兴市| 千阳县| 丹凤县| 安义县| 社旗县| 图木舒克市| 平舆县| 兴仁县| 黄浦区| 常州市| 呼伦贝尔市| 托里县| 新竹县| 措勤县| 色达县| 大足县| 新昌县| 乐昌市| 鄂尔多斯市| 阳城县| 达日县| 丰顺县| 龙井市| 浙江省| 衡阳县| 浮梁县| 察隅县| 阜新市| 汉阴县| 右玉县| 上思县| 读书| 偏关县| 璧山县| 贵港市| 长宁县| 鹤山市| 汝州市| 潮州市| 璧山县|