finally一定会执行吗?

在Sun Tutorial中有这样一句话:The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.  看来finally块中的语句应该是总会执行的。

  先来写一个最常见的写法:

 

[java]  view plain copy
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         try {  
  4.             System.out.println(args[0]);  
  5.             System.out.println("I'm nomal");  
  6.         } catch (Exception ex) {  
  7.             System.out.println("I'm exception");  
  8.         } finally {  
  9.             System.out.println("I'm finally.");  
  10.         }  
  11.     }  
  12. }  

  运行这段代码,很明显,不论是否有参考输入,"I'm finally."这句话都会打印出来。这是最常用的写法,很显然与Tutorial中的说明是相符的。

  下面我们再进一步想一下,假如在try或是catch块中使用了return语句,那么会怎么样呢?

  我们将代码稍做修改:

 

[java]  view plain copy
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         try {  
  4.             System.out.println(args[0]);  
  5.             System.out.println("I'm nomal");  
  6.                         return;  
  7.         } catch (Exception ex) {  
  8.             System.out.println("I'm exception");  
  9.                         return;  
  10.         } finally {  
  11.             System.out.println("I'm finally.");  
  12.         }  
  13.     }  
  14. }  

  代码的修改很简单,只是在try和catch块的结束位置分别加了一个return语句。

  这样运行结果是什么呢?可能会有两种猜想了,或是直接退出,或是仍会打印"I'm finally."。验证真理的方法是实践,我们运行这段代码,看一下结果:

 

[xhtml]  view plain copy
  1. >java Test  
  2. I'm exception  
  3. I'm finally.  
  4.   
  5. >java Test hello  
  6. hello  
  7. I'm nomal  
  8. I'm finally.  

  上面分别是输入和不输入参数时运行的结果,很明显,finally中的代码还是执行了。那是不是说try和catch块中的return语句并不起作用吗?我们再次简单修改代码:

 

[java]  view plain copy
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         try {  
  4.             System.out.println(args[0]);  
  5.             System.out.println("I'm nomal");  
  6.                         return;  
  7.         } catch (Exception ex) {  
  8.             System.out.println("I'm exception");  
  9.                         return;  
  10.         } finally {  
  11.             System.out.println("I'm finally.");  
  12.         }  
  13.                 System.out.println("Out of try.");  
  14.     }  
  15. }  

  在try语句外面再加入一名打印代码,再次编译。

  编译错误,结果如下:

 

[xhtml]  view plain copy
  1. Exception in thread "main" java.lang.Error: Unresolved compilation problem:   
  2.     Unreachable code  

  提示代码不可达,看来return还是有用的,只是在退出方法呼叫之前,会先去执行finally中的代码。

 

  现在似乎说明了另外一个问题,是不是return语句还不够厉害,“让暴风雨来的更猛烈些吧”,我们再次修改代码,将return语句修改成System.exit(),看一下执行结果。

 

[java]  view plain copy
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         try {  
  4.             System.out.println(args[0]);  
  5.             System.out.println("I'm nomal");  
  6.                         System.exit(0);  
  7.         } catch (Exception ex) {  
  8.             System.out.println("I'm exception");  
  9.                         System.exit(0);  
  10.         } finally {  
  11.             System.out.println("I'm finally.");  
  12.         }  
  13.     }  
  14. }  

  运行代码,终于,"I'm finally."不见了。

  为什么System.exit()有这么强大的力量呢,让我们看一下API中的说明:exit(int status): Terminates the currently running Java Virtual Machine。原来是这样,JVM都被终止掉了,当然不会再执行finally中的语句了。

 

  下面是我们的结论:

  在不终止VM的情况下,finally中的代码一定会执行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值