finally 语句块的深度辨析

finally 语句块的深度辨析

观点一 finally 语句块一定会执行吗?

finally块并不一定会执行。

  1. 如果程序在执行到try语句之前就已经停止了,finally块不会被执行。也就是说,只有与 finally 相对应的 try 语句块得到执行的情况下,finally 语句块才会执行。
  2. try 语句块中执行了 System.exit (0) 语句,终止了 Java 虚拟机的运行,finally语句也不会被执行

The finally Block
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return,continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
Note: If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
翻译如下
finally块
当try块退出时,finally块始终执行。 这样可以确保即使发生意外异常,也可以执行finally块。 但是,finally块不仅对异常处理有用,它还使程序员避免因返回,继续或中断而意外跳过清理代码。 即使在没有异常的情况下,将清除代码放在finally块中始终是一个好习惯。
注意:如果在执行try或catch代码时JVM退出,则finally块可能不会执行。 同样,如果执行try或catch代码的线程被中断或杀死,即使整个应用程序继续运行,finally块也可能不会执行。

如果 try 语句块中有 return 语句,那么 finally 语句块是在 return 之前执行,还是在 return 之后执行呢?

12.4. Try, catch, and finally
You catch exceptions by enclosing code in Try blocks. The basic syntax for a Try block is:
try {
statements
} catch (exception_type1 identifier1) {
statements
} catch (exception_type2 identifier2) {
statements

} finally {
statements
}
where either at least one catch clause, or the finally clause, must be present. The body of the try statement is executed until either an exception is thrown or the body finishes successfully. If an exception is thrown, each catch clause is examined in turn, from first to last, to see whether the type of the exception object is assignable to the type declared in the catch. When an assignable catch clause is found, its block is executed with its identifier set to reference the exception object. No other catch clause will be executed. Any number of catch clauses, including zero, can be associated with a particular TRy as long as each clause catches a different type of exception. If no appropriate catch is found, the exception percolates out of the try statement into any outer try that might have a catch clause to handle it.
If a finally clause is present with a try, its code is executed after all other processing in the try is complete. This happens no matter how completion was achieved, whether normally, through an exception, or through a control flow statement such as return or break.
翻译:
12.4. Try, catch, and finally
您可以通过将代码包含在Try块中来捕获异常。 Try块的基本语法为:

try {
   statements
} catch (exception_type1 identifier1) {
   statements
} catch (exception_type2 identifier2) {
   statements
   ...
} finally {
   statements
}

必须至少存在一个catch子句或finally子句的地方。将执行try语句的主体,直到引发异常或主体成功完成为止。如果抛出异常,则从头到尾依次检查每个catch子句,以查看异常对象的类型是否可分配给catch中声明的类型。找到可分配的catch子句后,将使用其标识符设置为引用异常对象的方式执行其块。不会执行其他catch子句。只要每个子句捕获不同类型的异常,任何数量的catch子句(包括零)都可以与特定的Try关联。如果找不到合适的catch,则异常将从try语句渗出到任何可能具有catch子句来处理的外部try中。
如果try出现了finally子句,则在try中的所有其他处理完成后,将执行其代码。无论正常情况下、通过异常还是通过控制流语句(例如return或break)来实现完成,都将发生这种情况。

通过上面这段文字我们可以指导,不管 try 语句块正常结束还是异常结束,finally 语句块是保证要执行的。如果 try 语句块正常结束,那么在 try 语句块中的语句都执行完之后,再执行 finally 语句块。如果 try 中有控制转移语句(return、break、continue)呢?那 finally 语句块是在控制转移语句之前执行,还是之后执行呢?似乎从上面的描述中我们还看不出任何端倪,不要着急,后面的讲解中我们会分析这个问题。如果 try 语句块异常结束,应该先去相应的 catch 块做异常处理,然后执行 finally 语句块。同样的问题,如果 catch 语句块中包含控制转移语句呢? finally 语句块是在这些控制转移语句之前,还是之后执行呢?我们也会在后续讨论中提到。
看描述我们知道,即使是存在控制语句,例如return和break,finally块也一样会执行,但是执行顺序并没有被说明。我们是不是可以大胆猜测,finally语句先执行,然后再执行return语句呢?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值