黑马程序员技术交流社区

标题: 关于Try的小问题 [打印本页]

作者: 赵庆礼    时间: 2012-7-4 23:15
标题: 关于Try的小问题
本帖最后由  赵庆礼 于 2012-7-5 11:43 编辑

public  class Test {
     public static void main(String[] args) {
          System.out.println(new Test().test());;
     }
     static int test()
     {
          int n = 1;
          try
          {
              return n;
          }
          finally
          {
              ++n;
          }
     }
}
try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后?

作者: 付蛟龙    时间: 2012-7-4 23:51
finally里的代码一定会执行,是在return之前执行,你debug下就可以看到执行流程。
作者: 孙飞    时间: 2012-7-4 23:55
finally里的语句是一定会执行的,但是是在return前执行,但是如果finally里没有return语句的话,会把try里的结果返回去做最终结果,如果finally里有return语句的话,会以finally里返回的结果做为最终结果的。你题里是先执行++n,但是并没有返回,所以try里面返回的n仍是1,打印结果是1
作者: 张天天    时间: 2012-7-5 06:45
package exception;

public class ExceptionTest1 {
       
  public static void main(String[] args) {
       
          int  c=0 ;
          
          try {
                  
                  int  a = 3;
                  
                  int b = 20;
                  
                   c = a/b;
                  System.out.println("hello world");
                  
          }
          
          catch (ArithmeticException  e){
                  
                  e.printStackTrace();
          }
          
          finally{
                  
                  System.out.println("this  is   a   ArithmeticException");
          }
          
          
          System.out.println(c);
}

}
执行结果为hello world
this  is   a   ArithmeticException
0
第二个程序是
package exception;

public class ExceptionTest2 {
       
        public  void  method() throws Exception{
               
                System.out.println("hello world ");
               
                throw new Exception();
        }
       
      public static void main(String[] args)  {
               
              ExceptionTest2  test = new ExceptionTest2();
              
              try {
                        test.method();
                } catch (Exception e) {
                       
                        e.printStackTrace();
                }
              
              finally {
                      
                      System.out.println( "this  programe  is  over");
              }
        }

}
执行结果为
java.lang.Exception
        at exception.ExceptionTest2.method(ExceptionTest2.java:9)
        at exception.ExceptionTest2.main(ExceptionTest2.java:17)
hello world
this  programe  is  over
自己看一下就知道为什么了啊
作者: 韦念欣    时间: 2012-7-5 08:23
finally代码快中,不管是从try跳出前,还是才catch跳出前,都要执行finally的代码,即使是return语句,也要执行finally,除非你的代码直接结束虚拟机。
作者: 赵庆礼    时间: 2012-7-5 11:42
张天天 发表于 2012-7-5 06:45
package exception;

public class ExceptionTest1 {

谢谢,有点明白了
作者: 赵庆礼    时间: 2012-7-5 18:04
韦念欣 发表于 2012-7-5 08:23
finally代码快中,不管是从try跳出前,还是才catch跳出前,都要执行finally的代码,即使是return语句,也要 ...

谢谢版主
作者: 黄克帅    时间: 2012-7-5 21:32
应该说是在return 中执行。  会先执行 try里面  return 后面的语句  在执行 fanally里面的语句  最后在执行try里面的 return。 如果fanally里面有return , try里面的return就不会执行了
这个问题在11期有人回答的很详细。  各种情况都说了,你可以去找找。




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