黑马程序员技术交流社区

标题: 用代码证明,在try中写了return,是先执行return还是先执行fianlly [打印本页]

作者: 人心如水    时间: 2014-6-14 20:28
标题: 用代码证明,在try中写了return,是先执行return还是先执行fianlly
class Test8{
        void show( int x ){
                if( x < 0 ){
                        throw new RuntimeException("小于零");
                }
                if( x > 0 ){
                        throw new RuntimeException("大于零");
                }
}
        void  method(){
                try{
                        show(1);
                        return ;
                }
                catch( RuntimeException e ){
                        System.out.println("异常被捕获");
                }
                finally{
                        System.out.println("总会被执行   ");
                }       
}
        public static void main(String[] args){
                new Test8().method();
                System.out.print("over");
        }
}
自己写了个,不过不知道表现出来是先执行谁
作者: love~陌    时间: 2014-6-14 20:38
少侠,这是黑马入学测试题还是基础测试题来着,我看到题目瞬间觉得很熟悉
作者: liujie445012100    时间: 2014-6-14 20:55
先return后finally
作者: 人心如水    时间: 2014-6-14 21:11
liujie445012100 发表于 2014-6-14 20:55
先return后finally

大神写个代码证明下,这我随便写的无法证明
作者: 人心如水    时间: 2014-6-14 21:13
love~陌 发表于 2014-6-14 20:38
少侠,这是黑马入学测试题还是基础测试题来着,我看到题目瞬间觉得很熟悉 ...

不是随机抽到的吗,怎么我的题目好几个别人都做过
作者: love~陌    时间: 2014-6-14 21:15
人心如水 发表于 2014-6-14 21:13
不是随机抽到的吗,怎么我的题目好几个别人都做过

题库就那么大,人又那么多~
作者: See_the_sun    时间: 2014-6-14 23:14
本帖最后由 See_the_sun 于 2014-6-15 01:36 编辑

package it.test.xt;

class Test8{
        public static void main(String[] args) {
                System.out.println(method());
        }
        
        public static String method(){//返回类型是字符串。
        try{
               // show(1);
              System.out.println("try");
                return "执行return";
        }
        catch( RuntimeException e ){
                System.out.println("异常被捕获");
        }
        finally{
                System.out.println("总会被执行   ");
        }  
        return "代码结束";
        }

    void show( int x ){
        if( x < 0 ){
                throw new RuntimeException("小于零");
        }
        if( x > 0 ){
                throw new RuntimeException("大于零");
        }
}

}   
//用Debug调试,一步步执行下来看。当中还有点区别的。。如果return是一个赋值的他会先把值传递出来,最后再相对于整个方法调用
//调试输出: 总会被执行,执行retrun 。。。retrun 是在方法最后返回给调用对象的。记住这点即可
   

作者: rover0321    时间: 2014-6-15 00:40
  1. public class Finally_Return
  2. {

  3.     /**
  4.      * @param args the command line arguments
  5.      */
  6.     public static void main(String[] args)
  7.     {
  8.        new Demo().method();
  9.         System.out.println("main over");
  10.     }
  11.    
  12. }

  13. class Demo
  14. {
  15.     public void show(int x) throws Exception
  16.     {
  17.         if (x < 0)
  18.         {
  19.             throw new Exception("小于零");
  20.         }
  21.         else
  22.         {
  23.             throw new Exception("大于等于零");
  24.         }
  25.     }
  26.    
  27.     public void method()
  28.     {
  29.         try
  30.         {
  31.             show(1);
  32.             return;
  33.         }
  34.         catch (Exception e)
  35.         {
  36.             System.out.println("异常被捕获");
  37.         }
  38.         finally
  39.         {
  40.              System.out.println("总会被执行");
  41.         }
  42.         System.out.println("method() over");
  43.     }
  44. }
复制代码


结论:show(1)出现异常的时候程序就会跳转到catch 中,不会执行下面的语句。

QQ图片20140615003810.jpg (11.74 KB, 下载次数: 188)

运行结果

运行结果

作者: GoodBoy123    时间: 2014-6-15 01:13
执行到return时会看看有没有finally代码,如果有了就执行完后结束,如果没有的话就马上结束,而不再执行finally之后的代码.
作者: 武动奇迹    时间: 2015-7-22 12:49
学习下。




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