try-catch-finally小结

最近学生去面试,被一个sb面试官忽悠了。那个家伙竟然说try块执行后不会执行finally。今天在http://wangwangzhi.iteye.com/blog/1632842上看到这篇文章摘录过来,供大家借鉴下。


使用 Integer.valueOf 方法模拟异常,来说明关于 try catch finally 之间的问题。


示例 1:没有异常,正常情况

 

Java代码   收藏代码
  1. public   class  TryCatchFinally {  
  2.   
  3.     public   static   void  main(String[] args) {  
  4.         try  {  
  5.             System.out.println("try statement     :-)" );  
  6.             Integer.valueOf("2" );  
  7.         } catch  (NumberFormatException nfe) {  
  8.             System.out.println("catch statement   :-)" );  
  9.         } finally  {  
  10.             System.out.println("finally statement :-)" );  
  11.         }  
  12.     }  
  13. }  


示例 2:没有异常,正常情况,但是 try 语句里面添加 return 

Java代码   收藏代码
  1. public   class  TryCatchFinally {  
  2.   
  3.     public   static   void  main(String[] args) {  
  4.         try  {  
  5.             System.out.println("try statement     :-)" );  
  6.             Integer.valueOf("2" );  
  7.             return ;  
  8.         } catch  (NumberFormatException nfe) {  
  9.             System.out.println("catch statement   :-)" );  
  10.         } finally  {  
  11.             System.out.println("finally statement :-)" );  
  12.         }  
  13.     }  
  14. }  


示例 1 和 示例 2 的运行结果一致:

 



示例 3:制造异常

 

Java代码   收藏代码
  1. public   class  TryCatchFinally {  
  2.   
  3.     public   static   void  main(String[] args) {  
  4.         try  {  
  5.             System.out.println("try statement     :-)" );  
  6.             Integer.valueOf("bluetooth" );  
  7.         } catch  (NumberFormatException nfe) {  
  8.             System.out.println("catch statement   :-)" );  
  9.         } finally  {  
  10.             System.out.println("finally statement :-)" );  
  11.         }  
  12.     }  
  13. }  


示例 4:制造异常,catch 语句里面添加 return

 

Java代码   收藏代码
  1. public   class  TryCatchFinally {  
  2.   
  3.     public   static   void  main(String[] args) {  
  4.         try  {  
  5.             System.out.println("try statement     :-)" );  
  6.             Integer.valueOf("bluetooth" );  
  7.         } catch  (NumberFormatException nfe) {  
  8.             System.out.println("catch statement   :-)" );  
  9.             return ;  
  10.         } finally  {  
  11.             System.out.println("finally statement :-)" );  
  12.         }  
  13.     }  
  14. }  


示例 3 和 示例 4 的运行结果一致:

 



由此,可以看出以上四种情况,finally 语句都被执行了。


示例 5:制造异常,catch 语句里面添加 return


目的:验证finally代码块后面的代码是否会被执行 ??????

 

Java代码   收藏代码
  1. public   class  TryCatchFinally {  
  2.   
  3.     public   static   void  main(String[] args) {  
  4.         try  {  
  5.             System.out.println("try statement     :-)" );  
  6.             Integer.valueOf("bluetooth" );  
  7.         } catch  (NumberFormatException nfe) {  
  8.             System.out.println("catch statement   :-)" );  
  9.             return ;  
  10.         } finally  {  
  11.             System.out.println("finally statement :-)" );  
  12.         }  
  13.   
  14.         System.out.println("out of try...catch...finally statement" );  
  15.     }  
  16. }  


执行结果:

 



如果将 catch 语句里面添加 return 注释掉,执行结果:



示例 6:制造异常,try 语句里面添加 return 

Java代码   收藏代码
  1. public   class  TryCatchFinally {  
  2.   
  3.     public   static   void  main(String[] args) {  
  4.         try  {  
  5.             System.out.println("try statement     :-)" );  
  6.             Integer.valueOf("dd" );  
  7.             return ;  
  8.         } catch  (NumberFormatException nfe) {  
  9.             System.out.println("catch statement   :-)" );  
  10.         } finally  {  
  11.             System.out.println("finally statement :-)" );  
  12.         }  
  13.   
  14.         System.out.println("out of try...catch...finally statement" );  
  15.     }  
  16. }  


执行结果:

 



示例 7:无异常,try 语句里面添加 return 

Java代码   收藏代码
  1. public   class  TryCatchFinally {  
  2.   
  3.     public   static   void  main(String[] args) {  
  4.         try  {  
  5.             System.out.println("try statement     :-)" );  
  6.             Integer.valueOf("2" );  
  7.             return ;  
  8.         } catch  (NumberFormatException nfe) {  
  9.             System.out.println("catch statement   :-)" );  
  10.         } finally  {  
  11.             System.out.println("finally statement :-)" );  
  12.         }  
  13.   
  14.         System.out.println("out of try...catch...finally statement" );  
  15.     }  
  16. }  


执行结果(无异常,return 返回,所以finally代码块后面的语句就不会执行:-)

 



以上种种迹象表明:


finally 语句都被执行了,但是还有另外一种情况没有被验证,接着往下看 快哭了


就是在 try...catch... 里面退出 JVM :


 

Java代码   收藏代码
  1. public   class  TryCatchFinally {  
  2.   
  3.     public   static   void  main(String[] args) {  
  4.         try  {  
  5.             System.out.println("try statement     :-)" );  
  6.             Integer.valueOf("2eer" );  
  7.             System.exit(0 );  
  8.         } catch  (NumberFormatException nfe) {  
  9.             System.out.println("catch statement   :-)" );  
  10.             System.exit(0 );  
  11.         } finally  {  
  12.             System.out.println("finally statement :-)" );  
  13.         }  
  14.   
  15.         System.out.println("out of try...catch...finally statement" );  
  16.     }  
  17. }  

 


执行结果:



try 或者 catch 语句里面(任意一个)调用 System.exit ,那么 finally 语句就不会被执行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值