---
title: "try-catch-finally小结"
author: "lichun01"
date: "2012-08-24T10:02:54Z"
tags: []
categories: ["编程语言"]
slug: "try-catch-finally"
draft: false
lastmod: "2012-08-24T10:02:54Z"
id: 1653281
url: "https://lichun01.iteye.com/blog/1653281"
---

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

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

**示例 1：没有异常，正常情况**

 

Java代码  [![收藏代码](http://wangwangzhi.iteye.com/images/icon_star.png "try-catch-finally小结")]()

01. public   class  TryCatchFinally {
02. public   static   void  main(String\[] args) {
03. try  {
04. System.out.println("try statement     :-)" );
05. Integer.valueOf("2" );
06. } catch  (NumberFormatException nfe) {
07. System.out.println("catch statement   :-)" );
08. } finally  {
09. System.out.println("finally statement :-)" );
10. }
11. }
12. }

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

Java代码  [![收藏代码](http://wangwangzhi.iteye.com/images/icon_star.png "try-catch-finally小结")]()

01. public   class  TryCatchFinally {
02. public   static   void  main(String\[] args) {
03. try  {
04. System.out.println("try statement     :-)" );
05. Integer.valueOf("2" );
06. return ;
07. } catch  (NumberFormatException nfe) {
08. System.out.println("catch statement   :-)" );
09. } finally  {
10. System.out.println("finally statement :-)" );
11. }
12. }
13. }

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

 

![](http://my.csdn.net/uploads/201208/15/1345005046_6587.png "try-catch-finally小结")

**示例 3：制造异常**

 

Java代码  [![收藏代码](http://wangwangzhi.iteye.com/images/icon_star.png "try-catch-finally小结")]()

01. public   class  TryCatchFinally {
02. public   static   void  main(String\[] args) {
03. try  {
04. System.out.println("try statement     :-)" );
05. Integer.valueOf("bluetooth" );
06. } catch  (NumberFormatException nfe) {
07. System.out.println("catch statement   :-)" );
08. } finally  {
09. System.out.println("finally statement :-)" );
10. }
11. }
12. }

**示例 4：制造异常，catch 语句里面添加 return**

 

Java代码  [![收藏代码](http://wangwangzhi.iteye.com/images/icon_star.png "try-catch-finally小结")]()

01. public   class  TryCatchFinally {
02. public   static   void  main(String\[] args) {
03. try  {
04. System.out.println("try statement     :-)" );
05. Integer.valueOf("bluetooth" );
06. } catch  (NumberFormatException nfe) {
07. System.out.println("catch statement   :-)" );
08. return ;
09. } finally  {
10. System.out.println("finally statement :-)" );
11. }
12. }
13. }

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

 

![](http://my.csdn.net/uploads/201208/15/1345005267_4945.png "try-catch-finally小结")

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

**示例 5：制造异常，catch 语句里面添加 return**

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

 

Java代码  [![收藏代码](http://wangwangzhi.iteye.com/images/icon_star.png "try-catch-finally小结")]()

01. public   class  TryCatchFinally {
02. public   static   void  main(String\[] args) {
03. try  {
04. System.out.println("try statement     :-)" );
05. Integer.valueOf("bluetooth" );
06. } catch  (NumberFormatException nfe) {
07. System.out.println("catch statement   :-)" );
08. return ;
09. } finally  {
10. System.out.println("finally statement :-)" );
11. }
12. System.out.println("out of try...catch...finally statement" );
13. }
14. }

执行结果：

 

![](http://my.csdn.net/uploads/201208/15/1345005510_8361.png "try-catch-finally小结")

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

![](http://my.csdn.net/uploads/201208/15/1345005555_1351.png "try-catch-finally小结")

**示例 6：制造异常，try 语句里面添加 return** 

Java代码  [![收藏代码](http://wangwangzhi.iteye.com/images/icon_star.png "try-catch-finally小结")]()

01. public   class  TryCatchFinally {
02. public   static   void  main(String\[] args) {
03. try  {
04. System.out.println("try statement     :-)" );
05. Integer.valueOf("dd" );
06. return ;
07. } catch  (NumberFormatException nfe) {
08. System.out.println("catch statement   :-)" );
09. } finally  {
10. System.out.println("finally statement :-)" );
11. }
12. System.out.println("out of try...catch...finally statement" );
13. }
14. }

执行结果：

 

![](http://my.csdn.net/uploads/201208/15/1345005757_3211.png "try-catch-finally小结")

**示例 7：无异常，try 语句里面添加 return** 

Java代码  [![收藏代码](http://wangwangzhi.iteye.com/images/icon_star.png "try-catch-finally小结")]()

01. public   class  TryCatchFinally {
02. public   static   void  main(String\[] args) {
03. try  {
04. System.out.println("try statement     :-)" );
05. Integer.valueOf("2" );
06. return ;
07. } catch  (NumberFormatException nfe) {
08. System.out.println("catch statement   :-)" );
09. } finally  {
10. System.out.println("finally statement :-)" );
11. }
12. System.out.println("out of try...catch...finally statement" );
13. }
14. }

执行结果（无异常，return 返回，所以finally代码块后面的语句就不会执行：-）

 

![](http://my.csdn.net/uploads/201208/15/1345005046_6587.png "try-catch-finally小结")

以上种种迹象表明：

finally 语句都被执行了，但是还有另外一种情况没有被验证，接着往下看 ![快哭了](http://static.blog.csdn.net/xheditor/xheditor_emot/default/fastcry.gif "try-catch-finally小结")

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

 

Java代码  [![收藏代码](http://wangwangzhi.iteye.com/images/icon_star.png "try-catch-finally小结")]()

01. public   class  TryCatchFinally {
02. public   static   void  main(String\[] args) {
03. try  {
04. System.out.println("try statement     :-)" );
05. Integer.valueOf("2eer" );
06. System.exit(0 );
07. } catch  (NumberFormatException nfe) {
08. System.out.println("catch statement   :-)" );
09. System.exit(0 );
10. } finally  {
11. System.out.println("finally statement :-)" );
12. }
13. System.out.println("out of try...catch...finally statement" );
14. }
15. }

 

执行结果：

![](http://my.csdn.net/uploads/201208/15/1345006307_5859.png "try-catch-finally小结")

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