编程语言
首页 > 编程语言> > javascript – break和continue语句最终导致Nashorn中的块执行

javascript – break和continue语句最终导致Nashorn中的块执行

作者:互联网

我很疑惑为什么在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 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,java-8,nashorn
来源: https://codeday.me/bug/20190830/1769690.html