黑马程序员技术交流社区

标题: 在try中写了return,后面有写了finally,是先执行return还是finally [打印本页]

作者: alucard    时间: 2014-4-4 09:39
标题: 在try中写了return,后面有写了finally,是先执行return还是finally
本帖最后由 alucard 于 2014-4-6 00:24 编辑

在try中写了return,后面又写了finally,是先执行return还是先执行finally?请举例说明。

作者: 黄泉    时间: 2014-4-4 10:09
在异常体系中。
try{}catch(){}finally{}是一个处理语句中的问题方式。
class Demo
{
        public static void main(String[] args)
        {
                try
                {
                        return ;
                }
                catch (Exception e)
                {

                }
                finally
                {
                        System.out.println("a");
                }
                System.out.println("abc");
        }
}
就如这个代码,当执行到 try 中的 return 时,主线程就会返回一个值,同时主线程将停止运行。

try块中的内容是在无异常发生时执行到结束
catch块中的内容,是在try块中内容发生catch所声明的异常时,跳转到catch块执行
finally块则是无论是否发生异常,都会执行finally块的内容
所以,代码逻辑中有需要无论发生什么都必须执行的代码,则可以放在finally块中
例如:最常见的就是把关闭connection、释放资源等的代码放在finally块中
作者: 2528870651    时间: 2014-4-4 10:12
  1. class ExceptionTest
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 int n = fun(2);
  6.                 System.out.println("打印fun()函数返回的n值=  "+n);
  7.         }
  8.         public static int fun(int i)
  9.         {
  10.                 try
  11.                 {
  12.                         int m= i/0;
  13.                         return i++;
  14.                 }
  15.         
  16.                 catch (ArithmeticException e)
  17.                 {
  18.                         System.out.println("异常信息:"+e);
  19.                         System.out.println("catch 中的i = "+i);
  20.                         return i+3;   //返回的是 2+3, 而不是finally中对i的赋值再来加上3,
  21.                                                         //finally中对i的操作,不会影响此时catch中的return i+3

  22.                 }
  23.                
  24.                 finally
  25.                 {
  26.                         i++;
  27.                         i++;
  28.                         System.out.println("finally 执行  "+i);
  29.                         //return i+8;   //如果这里没注释
  30.                                                         //这里会返回12,而不会去返回catch中的  return i+3
  31.                 }
  32.         }
  33. }
复制代码

作者: 2528870651    时间: 2014-4-4 10:14
本帖最后由 2528870651 于 2014-4-4 10:21 编辑
  1. return i+3;   //返回的是 2+3, 而不是finally中对i的赋值再来加上3,
  2.                                                         //finally中对i的操作,不会影响此时catch中的return i+3
复制代码

看看我这个程序,也许就懂了。程序执行到上面这句话时,会暂时保存i+3的值,也就是5.然后去执行finally,如果finally中有return,就执行finally中的return,如果没有return的话就反过来再执行catch中的return。因为catch中的  return i+3 已经在前面执行的时候就暂时保存了,所以在finally中对i的赋值不会影响return的结果还是 会返回5.


讲得够清楚了吧??  应该能懂了!!!

作者: y200745    时间: 2014-4-4 10:19
先执行finally
因为在执行return之前它会检查一下后面是否有finally,没有就直接返回,有就先执行finally再返回

  1. public class A{

  2.         int  fun()
  3.         {
  4.                 int x=1;
  5.                 try
  6.                 {int a=6;
  7.                         System.out.println("return执行,x="+x++);
  8.                         return a;
  9.                 }
  10.                 catch(Exception  e)
  11.                 {
  12.                        
  13.                 }
  14.                 finally
  15.                 {   int a=9;
  16.                         System.out.println("finally执行,x="+x++);
  17.                         return a;
  18.                        
  19.                 }
  20.         }
  21.         public static void main(String[] args) {
  22.                 A a =new A();
  23.                 System.out.println(a.fun());

  24.         }

  25. }
复制代码
行结果为:

解释原因为:代码反映的情况是return首先被执行,finally后被执行,return并不是让结果马上返回,而是先把结果放到函数中,然后必须等待finally结果出来后在真正的返回,此时返回的结果就是finally当中的那个结果,也就是return 9


作者: 张治国    时间: 2014-4-4 10:19
本帖最后由 张治国 于 2014-4-4 10:26 编辑

我的这种情况是先执行finally,return不执行
原因:在执行return之前它会检查一下后面是否有finally,如果没有就直接返回,如果有就先执行finally,return就不执行了。
举例:


public class Test {
  public static String start()
  {
            try {
              return fun1();
               }
            finally {
                   return fun2();
                }
        }
   public static String fun1()
   {
           return "return被执行";      
   }
   public static String fun2()
   {
           return "finally被执行";        
   }
   public static void main(String[] args)
   {
          System.out.print(new Test().start());
      }
}

执行结果是finally被执行

QQ截图20140404101834.png (5.32 KB, 下载次数: 11)

QQ截图20140404101834.png

作者: 1453149997    时间: 2014-4-4 17:41
finally是在return执行的某个时刻执行的;
你的这个问题的答案可以从网上搜,和一道面试题很类似。
作者: 888_loveyou    时间: 2014-4-5 11:43
楼上的说的都很好啊。。
作者: Saner    时间: 2014-4-5 13:34
return首先被执行,finally后被执行,return并不是让结果马上返回,而是先把结果放到函数中,然后必须等待finally结果出来后在真正的返回,此时返回的结果就是finally当中的那个结果






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2