【发布时间】:2018-08-02 04:17:49
【问题描述】:
如果 catch 块之后的语句无论如何都要执行,那么 finally 块在 java 中的真正用途是什么? 示例
try {
//Code
}
catch (Exception e)
{
//Code
}
finally {
System.out.println("anyway it will be executed");
}
System.out.println("anyway it will be executed");
【问题讨论】:
-
代码中的任何地方都可能有return语句。即使代码可能已经返回到调用方法,
finally块中的代码仍然会被执行