我很疑惑为什么在break / continue之后最终会被执行(版本:Java 8 update 5)
testTryFinally();
function testTryFinally()
{
try
{
print("Try 1");
for(var i = 0; i < 2; i++)
{
break;
}
print("Try 2");
}
finally
{
print("Finally executed");
}
}
执行:
… / tmp> jjs test.js
输出中断:
Try 1
Finally executed
Try 2
Finally executed
输出继续代替休息
Try 1
Finally executed
Finally executed
Try 2
Finally executed
我尝试在浏览器中执行相同的代码(用console.log()替换print()
输出与Firebug中的预期一致
Try 1
Try 2
Finally executed
解决方法:
我找到了相关声明here:
. . . While it’s true
finally
will always be executed if defined, certain statements insidetry
such ascontinue
,break
,return
, or when an error has occurred and there is no catch clause will all causefinally
to be executed immediately thereafter . . .
但是我没有看到这种情况在Firefox中发生(可能是有充分理由的)