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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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");
        }
}
自己写了个,不过不知道表现出来是先执行谁

9 个回复

倒序浏览
少侠,这是黑马入学测试题还是基础测试题来着,我看到题目瞬间觉得很熟悉
回复 使用道具 举报
先return后finally
回复 使用道具 举报

大神写个代码证明下,这我随便写的无法证明
回复 使用道具 举报
love~陌 发表于 2014-6-14 20:38
少侠,这是黑马入学测试题还是基础测试题来着,我看到题目瞬间觉得很熟悉 ...

不是随机抽到的吗,怎么我的题目好几个别人都做过
回复 使用道具 举报
人心如水 发表于 2014-6-14 21:13
不是随机抽到的吗,怎么我的题目好几个别人都做过

题库就那么大,人又那么多~
回复 使用道具 举报
本帖最后由 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 是在方法最后返回给调用对象的。记住这点即可
   
回复 使用道具 举报
  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, 下载次数: 184)

运行结果

运行结果
回复 使用道具 举报
执行到return时会看看有没有finally代码,如果有了就执行完后结束,如果没有的话就马上结束,而不再执行finally之后的代码.
回复 使用道具 举报
学习下。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马