try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后?
很多人都说是在return前执行,但我的写的测试程序确得到不同答案?有没有高手讲讲啊
public String message(){
System.out.println("returned");
return "123";
}

public String testFinal(){
try{
return message();
}catch(Exception e){
e.printStackTrace();
return "exception";
}finally{
System.out.println("do finally");
}
}

public static void main(String[] args){
Test test = new Test();
test.testFinal();
}

解决方案 »

  1.   

    finally是一定会执行的只有代码中有system.exit() 这一种情况 才不会执行finally因为终止了虚拟机进程
    finally会在return之前执行如果在finally里面有return情况会更复杂
      

  2.   

    先调用message()函数,但此时还不会return,执行finally{}完后,就只剩下return了,return执行就跳出该函数。你可以设断点,按F6 
    debug下,可以清楚看到。
      

  3.   

    finally的代码会在return前执行.
      

  4.   

    会执行在 try 中 或 catch 中遇到 return 就会转入 finally
      

  5.   


    楼主说到的 "finally会在return之前执行",小弟不认同,经测试过,
    结果跟楼主的描述是相反的.结论是: 如果finally之前没有执行过:System.exit(0); 则 finally 是一定执行,并且是最后执行
      

  6.   

    先执行return,后执行finally中的代码
      

  7.   

    看看这个http://topic.csdn.net/t/20041211/20/3636856.html理解一下什么是--------return-----------
      

  8.   

    还有这个http://blog.csdn.net/gaoyuanfeng/archive/2009/04/11/4064388.aspx
      

  9.   

    public String message(){
    System.out.println("returned");
    return "123"; ----  2
    }public String testFinal(){
    try{
    return message();   ----  1
    }catch(Exception e){
    e.printStackTrace();
    return "exception";
    }finally{
    System.out.println("do finally");   ---  3
    }
    }public static void main(String[] args){
    Test test = new Test();
    test.testFinal();
    }感觉结果应该是:
    returned
    do finally
      

  10.   

    根据java规范:在try-catch-finally中,如果try-finally或者catch-finally中都有return,则两个return语句都执行并且最终
    返回到调用者那里的是finally中return的值;而如果finally中没有return,则理所当然的返回的是try或者catch中return的值,但是
    finally中的代码是必须要执行的,而且是在return前执行,除非碰到exit()。
      

  11.   


    public String message(){
    System.out.println("returned");
    return "123"; ----  2
    }public String testFinal(){
    try{
    return message();  ----  1
    }catch(Exception e){
    e.printStackTrace();
    return "exception";
    }finally{
    System.out.println("do finally");  ---  3
    }
    }
    public static void main(String[] args){
    Test test = new Test();
    test.testFinal();
    } 土匪说的很对  finally 确实在return 前执行  在我标注的 2  的return 前执行
      

  12.   


    public String message() {
    System.out.println("returned");
    return "123";
    } public String testFinal() {
    try {
    return message();
    } catch (Exception e) {
    e.printStackTrace();
    return "exception";
    } finally {
    System.out.println("do finally");
    }
    } public static void main(String[] args) {
    Test test = new Test();
    System.out.println(test.testFinal());
    }你吧程序改成 如上 就很清楚的看出 执行顺序了
      

  13.   


    public class Test2 {
    public static void main(String[] args) {
    System.out.print("in main b = " + tt());
    } public static int tt() {
    int b = 23;
    try {
    return b = 88;
    } finally {
    System.out.println("in finally b = " + b);
    b = 66;
    System.out.println("after being assigned, b = " + b);
    return b = 44;
    }
    }
    }
    in finally b = 88
    after being assigned, b = 66
    in main b = 44
    返回值和return(退出函数)确实是两回事
    http://blog.csdn.net/zcjl/archive/2004/12/12/214123.aspx 
      

  14.   

    String str = "1";
    public String testFinal(){
    try{
    return str;
    }catch(Exception e){
    e.printStackTrace();
    return "exception";
    }finally{
    str += "0";
    }
    }public static void main(String[] args) {     Test test = new Test();
         System.out.println(test.testFinal());
    }
    为什么这句str += "0";执行了,打印是1呢?
      

  15.   

    刚写了点东西,
    http://blog.csdn.net/ZangXT/archive/2009/05/21/4206672.aspx
    自己看一下吧。
      

  16.   


    规范就是规范。。如果try先有return,之后捕获到了异常转入catch后又有return。那执行那条??
      

  17.   

    谢谢大家了 我明白了 虽然return 语句执行了message方法里的代码,但并没有执行return,执行完finally代码后才去执行的return 我看现在分不够分给大家的,明天再加30,结贴
      

  18.   

    finally语句,只有一种情况不会被执行,那就是断电了
      

  19.   

    你错了!
    finally是一定执行的,并且在return前执行!
      

  20.   


    但如果return是一个表达式的话,是先要执行这个表达式的。最后return只是说整个函数返回一个东西。然后函数退出。
      

  21.   

    package com.zxm.log4j;import java.io.File;/**
    *try中遇到return语句的话,先计算表达式的值,再执行finally,最后把刚才try中计算的值返回(注意,最后退出的时候不再重新计算)! */
    public class Test { public static void main(String[] args) {
    System.out.println(getPath());
    System.out.println(getPath1());
    System.out.println(getPath2());
    }
    public static String getPath() {
    File f = new File("");
    try {
    int j = 3;
    String path = "";
    path = f.getAbsolutePath();
    return f.getAbsolutePath();//try中遇到return语句的话,先计算表达式的值,在finally,最后把刚才try中计算的值返回(注意,最后退出返回的时候不再重新计算)!
    } finally {
    f = null;
    }
    } public static String getPath1() {
    File f = new File("/");
    try {
    int j = 3;
    String path = "";
    path = f.getAbsolutePath();
    String path2;
    return path2 = path + "5555";
    // int i=2;//error
    } finally {
    f = null;
    }
    } /**
     * 从系统变量得到当前绝对路径
     * 
     * @return
     */
    public static String getPath2() {
    return System.getProperty("user.dir");
    } public static String getPath3() {
    return Test.class.getResource("").toString();
    } public static String getPath3_1() {
    return Test.class.getResource("").getPath();
    } public static String getPath4() {
    return Test.class.getResource("/").toString();
    } public static String getPath4_1() {
    return Test.class.getResource("/").getPath();
    } public static String getPath5() {
    return Thread.currentThread().getContextClassLoader().getResource("")
    .toString();
    } public static String getPath5_1() {
    return Thread.currentThread().getContextClassLoader().getResource("")
    .getPath();
    } public static String getPath6() {
    return Thread.currentThread().getContextClassLoader().getResource("/")
    .toString();
    } public static String getPath6_1() {
    return Thread.currentThread().getContextClassLoader().getResource("/")
    .getPath();
    }}
      

  22.   

    public  class Test {
    static private int x;
    public static void main(String[] args) {
    Test test = new Test();
    System.out.println(test.test());
    System.out.println(test.test()); }
            static int test()
    {
    try
    {
    return x;
    }
    finally
    {
     System.out.println("abc");
      x++;
    }
    }
    }
    在return前执行