一个比较古老的话题,try{}中有return,finally{}中的代码是否还执行?在return之前执行还是之后?
我的看法是肯定执行,但是应该是return之后执行,下面是测试代码,不知道我的理解对不对,请大家指正。public static void main(String []args){
System.out.println(fun());
}
public static int  fun(){
int i=0; //避开特殊的String
try
{
System.out.println("try");
return i;
}
catch(Exception e)
{
System.out.println("catch");
}
finally
{
i++;
System.out.println("finally");

return i;
}执行后显示:
try
finally
0finally输出了,但是i并没有自加,我想应该是finally执行时i已经返回

解决方案 »

  1.   

    finally在跟在trycatch语句后面的 肯定要执行 只是在return前还是后 lz的想法应该是对的 学习中
      

  2.   

    你输出的是try里面的i,不是finally的i。finally在return之前执行的。
      

  3.   

    finally的代码是在return之前执行的,具体的解释,lz请看http://www.360doc.com/content/05/1013/09/73_18906.shtml
      

  4.   

    finally的代码是在return之前执行的,具体的解释,lz请看http://www.360doc.com/content/05/1013/09/73_18906.shtml
      

  5.   

    try在执行到return时   接着执行final语句  但此时return i;记住的是i=0这个值
    return的值不会再因为final中i的改变而改变
       
      

  6.   

    public static void main(String []args){
            System.out.println(fun());
        }
        public static int  fun(){
            int i=0;        //避开特殊的String
            try
            {
                System.out.println("try");
                return i;
            }
            catch(Exception e)
            {
                System.out.println("catch");
            }
            finally
            {
                i++;
                System.out.println("finally");
                return i;
            } 
            //return i;
        }
    return放在finally外面返回的i当然不是“+1”后的i。这样很清楚吧。
      

  7.   

    楼主的理解是不对的,return是在finally之后执行的。你可以在
    try
            {
                System.out.println("try");
                return i;
            }中的return i 断点进行debug去查看它的执行顺序。
      

  8.   


    public static void main(String []args){
            System.out.println(fun());
        }
        public static int fun(){
            int i=0;        //避开特殊的String
            try
            {
                System.out.println("try");
                
                return i;
            }
            catch(Exception e)
            {
                System.out.println("catch");
            }
            finally
            {
                ++i;
                System.out.println("finally");
            } 
            
            System.out.println("do");
            return i;
        }在finally以后的语句都没有执行System.out.println("do");  return i; 这两句根本就没有执行.....
      

  9.   

    关键还是值的问题,如果你理解了java调用方法时的pass by value,这个问题也就解决了
      

  10.   

    A finally clause is always entered with a reasonThat reason may be that the try code finished normally, that it executed a control flow statement such as return, or that an exception was thrown in code executed in the TRy block. The reason is remembered when the finally clause exits by falling out the bottom. However, if the finally block creates its own reason to leave by executing a control flow statement (such as break or return)or by throwing an exception, that reason supersedes the original one, and the original reason is forgotten . For example, consider the following code:try {
        // ... do something ...
        return 1;
    } finally {
        return 2;
    }When the TRy block executes its return, the finally block is entered with the "reason" of returning the value 1. However, inside the finally block the value 2 is returned, so the initial intention is forgotten. In fact, if any of the other code in the try block had thrown an exception, the result would still be to return 2. If the finally block did not return a value but simply fell out the bottom, the "return the value 1" reason would be remembered and carried out.——摘自《THE Java™ Programming Language, Fourth Edition》By Ken Arnold, James Gosling, David Holmes
      

  11.   

    public class Mian { public static void main(String[] args) {
    System.out.println(fun());
    } public static int fun() {
    int i = 0; // 避开特殊的String
    try {
    System.out.println("try");
    return returning(i); } catch (Exception e) {
    System.out.println("catch");
    } finally {
    i++;
    System.out.println("finally");
    } return i;
    } private static int returning(int i) {
    System.out.println("returning");
    return i;
    }
    }执行结果:
    try
    returning
    finally
    0
      

  12.   

    如果终止程序的System.exit()方法在保护码内被执行,那么,这是finally语句不被执行的唯一情况。这就暗示,控制流程能偏离正常执行顺序,比如,如果一个return语句被嵌入try块内的代码中,那么,finally块中的代码应在return前执行。
      

  13.   

    这段代码会变成  int i = 0;
            int j;
            System.out.println("try");
            j = i;
            i++;
            System.out.println("finally");
            return j;
            Exception e;
            e;
            System.out.println("catch");
            i++;
            System.out.println("finally");
            break MISSING_BLOCK_LABEL_62;
            Exception exception;
            exception;
            i++;
            System.out.println("finally");
            throw exception;
            return i;
      

  14.   

    楼主误解了 try 可以分为两种情况:有异常 和 没异常
    首先,有异常的情况:
    try 中的代码 无论是什么 只要记住在哪里异常 代码就在哪里终止(比如是System.out.println("try");
    ) 终止之后 后面的代码(return(i))是执行不到的 是执行catch代码块的 最后一定必定会执finally代码块
    然后,没异常的情况:
    try中代码全部执行完之后直接跳过catch代码块,执行finally代码块。
    就这么简单。
      

  15.   

    我的看法和楼主不同。
    根据我看书的结果,我曾经详细的看到过书中对于楼主问题的详细解释,可惜这本书借来的,不再手上了,书中是这样说的,return()看似是一个方法,但是其实虚拟机是分成两部分进行的,在return()的第一个阶段,回去执行finally(你设置的情况下),然后返回结果值。果如非要说谁在谁前面,那就大概地说finally是在return之前的
      

  16.   


    源代码
    int i=0;
            try
            {
                System.out.println("try");
                return i;
            }
            catch(Exception e)
            {
                System.out.println("catch");
            }
            finally
            {
                i++;
                System.out.println("finally");
                return i;
            }jvm优化后的代码
    int i = 0;
            try
            {
                System.out.println("try");
            }
            catch(Exception e)
            {
                System.out.println("catch");
            }
            finally
            {
                i++;
                System.out.println("finally");
                return i;
            }
    这么改就正常了
      

  17.   

    LZ理解有问题,用事实来说话:
    Situation 1.
    public class Return_finally {
       
       public static int fun() {
       int i=0;
       try{
       System.out.println("try");
       return i;
       } catch(Exception e) {
       System.out.println("catch");
       } finally {
       i++;
       System.out.println("finally");
       }
       return i;
       }
           public static void main(String args[]) {
            System.out.println(fun());
           }
    }
      
    结果:try
    finally
    0Situation 2.public class Return_finally {
       
       public static int fun() {
       int i=0;
       try{
       i=1/0;
       System.out.println("try");
       return i;
       } catch(Exception e) {
       System.out.println("catch");
       } finally {
       i++;
       System.out.println("finally");
       }
       return i;
       }
           public static void main(String args[]) {
            System.out.println(fun());
           }
    }结果:catch
    finally
    1Situation 3.public class Return_finally {
       
       public static int fun() {
       int i=0;
       try{
      
       System.out.println("try");
       return i;
       } catch(Exception e) {
       System.out.println("catch");
       } finally {
       i++;
       System.out.println("finally");
       return i;
       }
       
       }
           public static void main(String args[]) {
            System.out.println(fun());
           }
    }
     结果:try
    finally
    1Situation 4.public class Return_finally {
       
       public static int fun() {
       int i=0;
       try{
       i=1/0;
       System.out.println("try");
       return i;
       } catch(Exception e) {
       System.out.println("catch");
       } finally {
       i++;
       System.out.println("finally");
       return i;
       }
      
       }
           public static void main(String args[]) {
            System.out.println(fun());
           }
    }结果:catch
    finally
    1Situation 5.public class Return_finally {
       static int i=0;
       public static int fun() {
       try{
       System.out.println("try");
       return i;
       } catch(Exception e) {
       System.out.println("catch");
       } finally {
       i++;
       System.out.println("finally");
       }
    return i;
       }
           public static void main(String args[]) {
            System.out.println(fun());
            System.out.println("now i="+i);
           }
    }
      结果:try
    finally
    0
    now i=1相信这么明显,应该没人看不懂了吧!其实还有其它情况,但是有些情况会语法出错,可自己试试
      

  18.   

    感觉这个要看看你的try里有没有发生异常吧..异常发生在return前还是后吧当然return后不可能有代码的,如果return前没有异常.就先return.然后再finally
      

  19.   

    俺分解一下代码,其实java编译后就是类似于这种流程的:public static int fun(){                     public static int fun(){              
        int i=0; //避开特殊的String                  int i=0; //避开特殊的String       
        try  {                                       try  {                            
            System.out.println("try");                   System.out.println("try");    
                                                         // 执行过程可以看成这样:                              
            return i;                                    int local = i;   // 把返回值存入一个本地变量中
                                                         return local;    // 返回保存返回值的本地变量的值
        } catch(Exception e) {                       } catch(Exception e) {            
            System.out.println("catch");                 System.out.println("catch");  
        } finally  {                                 } finally  {                      
            i++;                                         i++;                          
            System.out.println("finally");               System.out.println("finally");
        }                                            }                                 
        return i;                                    return i;                         
    }                                            }
    java1.5后finally中的语句是以“内联”形式执行的,不再是之前的以“微型子例程”的形式执行,内联的形式如下:public static int fun(){                                                   
        int i=0; //避开特殊的String       
        try  {                            
            System.out.println("try");    
            // 执行过程可以看成这样:     
            int local = i; // 把返回值存入一个本地变量中
            
            //以下是finally中的语句,以内联形式执行 -- finally begin
            i++;                          
            System.out.println("finally");
            //-----------------------------------------finally end
                            
            return local; //注意这里的return,返回的是之前保存返回值的本地变量的值,并不是 i;                
        } catch(Exception e) {            
            System.out.println("catch");
            //以下是finally中的语句,以内联形式执行 -- finally begin
            i++;                          
            System.out.println("finally");
            //-----------------------------------------finally end     
        } 
        //finally  {                      
        //    i++;                          
        //    System.out.println("finally");
        //}                                 
        return i;                         
    }