隨筆-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 閱讀(2279) 評論(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

            回復  更多評論
            
          <2009年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類(204)

          隨筆檔案(100)

          收藏夾(8)

          牛人博客鏈接

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 松原市| 东乌| 灵川县| 阜新市| 河津市| 永宁县| 思南县| 武清区| 阳春市| 苗栗市| 鹰潭市| 凌云县| 八宿县| 乌拉特中旗| 赤峰市| 乐清市| 江达县| 彝良县| 英德市| 珠海市| 南岸区| 临夏县| 崇礼县| 山西省| 汽车| 阿克| 锦屏县| 湘阴县| 侯马市| 两当县| 上杭县| 那坡县| 司法| 榆林市| 伊川县| 原平市| 抚远县| 六枝特区| 读书| 北辰区| 读书|