【发布时间】:2013-05-01 00:54:58
【问题描述】:
我有这个带有嵌套尝试的 java 代码:
try
{
try
{
[ ... ]
{
catch (Exception ex)
{
showLogMessage(ex);
return;
}
while (condition == true)
{
try
{
[ ... ]
{
catch (Exception ex)
{
showLogMessage(ex);
continue;
}
[ ... ]
}
}
catch (NumberFormatException e)
{
showLogMessage(e);
}
finally
{
doSomeThingVeryImportant();
}
我想知道当我遇到异常时是否总是执行finally。我问这个是因为 catch 块有 return 或 continue 语句。
doSomeThingVeryImportant() 何时执行?当我收到NumberFormatException 时收到Exception 时?
我只希望在执行任何 catch 块之后,也执行 finally 块。
【问题讨论】:
-
出于好奇和扩展问题:即使在
exit语句之后最终执行? -
当您遇到异常时,您会运行您的代码并亲自查看它是否执行!我真的怀疑docs 中不存在的任何内容是否已发布在以下任何答案中!
-
请停止投票。这不是一个重复的问题。