转:finally块中的代码一定会执行…

来源:互联网 发布:淘宝店铺排名怎么看 编辑:程序博客网 时间:2024/05/22 05:24
源地址:http://www.cnblogs.com/thinkCoding/archive/2010/04/29/1724419.html

finally块中的代码一定会执行吗

在SunTutorial中有这样一句话:The finally block always executeswhen the try blockexits. This ensures thatthe finally block isexecuted even if an unexpected exception occurs. 看来finally块中的语句应该是总会执行的。

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

 

代码
复制代码
publicclass Test{
publicstaticvoid main(String[]args) {
try {
System.out.println(args[
0]);
System.out.println(
"I'mnomal");
catch (Exceptionex) {
System.out.println(
"I'mexception");
finally {
System.out.println(
"I'mfinally.");
}
}
}
复制代码

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

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

  我们将代码稍做修改:

 

复制代码
publicclass Test{
publicstaticvoid main(String[]args) {
try {
System.out.println(args[
0]);
System.out.println(
"I'mnomal");
return;
catch (Exceptionex) {
System.out.println(
"I'mexception");
return;
finally {
System.out.println(
"I'mfinally.");
}
}
}
复制代码

 

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

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

 

复制代码
>javaTest
I
'mexception
I'mfinally.

>javaTest hello
hello
I
'mnomal
I'mfinally.
复制代码

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

代码
复制代码
publicclass Test{
publicstaticvoid main(String[]args) {
try {
System.out.println(args[
0]);
System.out.println(
"I'mnomal");
return;
catch (Exceptionex) {
System.out.println(
"I'mexception");
return;
finally {
System.out.println(
"I'mfinally.");
}
System.out.println(
"Outof try.");
}
}
复制代码

 

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

  编译错误,结果如下: 

  Exception in thread "main" java.lang.Error: Unresolved compilation problem:    

  Unreachable code 


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

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

代码
复制代码
publicclass Test{
publicstaticvoid main(String[]args) {
try {
System.out.println(args[
0]);
System.out.println(
"I'mnomal");
System.exit(
0);
catch (Exceptionex) {
System.out.println(
"I'mexception");
System.exit(
0);
finally {
System.out.println(
"I'mfinally.");
}
}
}
复制代码

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

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

  下面是我们的结论:

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

 

向原作者致敬!

原创粉丝点击