A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 任传敏 初级黑马   /  2012-6-23 14:58  /  2184 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

对于一个方法中的
try{
return num;
}catch(){
}finally{
int num++
}
据说finally块中的语句一定会被执行,但是如果try块中有return语句,会怎么样呢!?

评分

参与人数 1技术分 +1 收起 理由
职业规划-刘倩老师 + 1 新手报到,鼓励一下

查看全部评分

9 个回复

倒序浏览
  1. public class Test {
  2.         public static void main(String[] args){
  3.                 System.out.println(method(5));       
  4.         }
  5.         public static int method(int num){
  6.                 try{
  7.                         return num;
  8.                 }finally{
  9.                         System.out.println("fially执行了");
  10.                         num++;
  11.                 }
  12.         }
  13. }
复制代码
结果
fially执行了
5
回复 使用道具 举报
上面那个看不出效果
  1. public class Test {
  2.         public static void main(String[] args){
  3.                 System.out.println(method(5));       
  4.         }
  5.         public static int method(int num){
  6.                 try{
  7.                         return num;
  8.                 }finally{
  9.                         System.out.println("fially执行了"+ ++num);
  10.                 }
  11.         }
  12. }
复制代码
fially执行了 6

点评

用心解答,建议刘老师加分!  发表于 2012-6-24 22:37
回复 使用道具 举报
try{
return num;
}catch(){
}finally{
int num++
}
finally是一定会执行的,但是如果在try里面加了return,那么得到的值还是num,返回后执行num++,所以最后num的值还是加了1。
回复 使用道具 举报
public static void main(String[] args) {
          System.out.println(new Test().test());
    }

     static int test()
     {
          int x = 1;
          try
          {
              return x;
          }
          finally
          {
              ++x;
          }
     }
运行结果是1,为什么呢?主函数调用子函数并得到结果的过程,好比主函数准备一个空罐子,当子函数要返回结果时,先把结果放在罐子里,然后再将程序逻辑返回到主函数。所谓返回,就是子函数说,我不运行了,你主函数继续运行吧,这没什么结果可言,结果是在说这话之前放进罐子里的。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class smallT
{
      public static void main(String args[])
     {
          smallT t = new smallT();
          int b = t.get();
          System.out.println(b);
     }
     public int get()
     {
         try
         {
             return 1 ;
         }
          finally
         {
             return 2 ;
         }
     }
}

返回的结果是2。


我可以通过下面一个例子程序来帮助解释这个答案,从下面例子的运行结果中可以发现,try中的return语句调用的函数先于finally中调用的函数执行,也就是说return语句先执行,finally语句后执行,所以,返回的结果是2。Return并不是让函数马上返回,而是return语句执行后,将把返回结果放置进函数栈中,此时函数并不是马上返回,它要执行finally语句后才真正开始返回。

public class Test {
/**
* @param args add by zxx ,Dec 9, 2008
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new Test().test());;
}
int test()
{
try
{
return func1();
}
finally
{
return func2();
}
}
int func1()
{
System.out.println("func1");
return 1;
}
int func2()
{
System.out.println("func2");
return 2;
}
}
-----------执行结果-----------------
func1
func2
2
结论:finally中的代码比return 和break语句后执行
回复 使用道具 举报
public static void main(String[] args) throws Exception {
                // TODO Auto-generated method stub
                        System.out.println(method(5));        
                }
                public static int method(int num) throws Exception{
                        try{
                                return num;
                        }catch(Exception e){
                                 throw new Exception("num=null",e);
                        }
                        finally{
                                System.out.println("fially执行了");
                                num++;//
                                //return num;//要是这返回结果就是6.
                                System.out.println(num);//当finally里没有返回,在这输出的是6,在主函数里输出的是5.
                        }
                }
执行结果是:
fially执行了
6
5
楼主要明白:
虽然返回了,但上边的执行顺序是执行完finally再执行主函数里的代码,那个返回值是编译器存储到内存中了不会变。
但当你finally里再有返回的话,它会将前边的覆盖掉。

点评

清晰明白,收益匪浅!  发表于 2012-6-24 22:35
回复 使用道具 举报
楼主,finally 块中的语句是一定会执行的,这点是肯定的,下边说说真正实战中finally常常用于关闭资源的工作,比如说在与数据库连接时finally中就会写 connection。close();这样类似的关闭资源的语句。
finally是在return 前执行的,就拿你这个例子来说吧,他在try快中的return num前执行了finally块中的语句吧num的值进行+1运算以后才会返回到try块中执行return num;语句。
回复 使用道具 举报
try{
return num;
}catch(){
}finally{
int num++
}
当try中出现retun时,finanly当中的语句还是被执行,因为finaly中的语句一定会被执行,但是当try/catch中出现System.exit(0);时,finaly才不会被执行
回复 使用道具 举报
游洪波 发表于 2012-6-23 16:01
楼主,finally 块中的语句是一定会执行的,这点是肯定的,下边说说真正实战中finally常常用于关闭资源的工 ...

兄弟你的理解有点问题,我刚试了下
public class Test2
{
      public static void main(String args[])
     {
              Test2 t = new Test2();
          int b = t.get(3);
          System.out.println(b);
     }
     public int get(int a)
     {
         try
         {
             return a ;
         }
          finally
         {
                  System.out.println("fially执行了"+ ++a);
         }
     }
}
运行结果是:
fially执行了4
3
如果按你的理解的话应该输出两个4吧
回复 使用道具 举报
public class Test {
        public static void main(String[] args){
                System.out.println(method(5));        
        }
        public static int method(int num){
                try{
                        return num;
                }finally{
                        System.out.println("fially执行了"+ ++num);
                }
        }
}

5
6
除了用到关闭电源,否则不管用到什么finally里的代码都会执行
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马