gpt4 book ai didi

javascript - break 和 continue 语句导致 Nashorn 中的 finally block 执行

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:05:18 28 4
gpt4 key购买 nike

我很困惑为什么 finallybreak/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 测试.js

break的输出:

Try 1
Finally executed
Try 2
Finally executed

continue 代替 break 输出

Try 1
Finally executed
Finally executed
Try 2
Finally executed

我尝试在浏览器中执行相同的代码(将 print() 替换为 console.log()

Firebug 中的输出符合预期

Try 1
Try 2
Finally executed

最佳答案

找到相关说法here :

. . . While it's true finally will always be executed if defined, certain statements inside try such as continue, break, return, or when an error has occurred and there is no catch clause will all cause finally to be executed immediately thereafter . . .

但我没有看到这在 Firefox 中发生(可能有充分的理由)

关于javascript - break 和 continue 语句导致 Nashorn 中的 finally block 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24719066/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com