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

标签 javascript java-8 nashorn

我很困惑为什么 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/

相关文章:

java - 在 Java 8 中尝试通过 JDBC-ODBC 连接到 .accdb 文件时出现 ClassNotFoundException

javascript - Java 8 Nashorm 引擎无法从 json 对象获取 key

javascript - Java Nashorn 使用 webEngine 事件

java-8 - 最新的 JDK 8 Build 40 从 Nashorn 引擎返回间歇性异常

函数参数的javascript闭包在更新时不会反射(reflect)出来

javascript - 如何让 Javascript 将通过 AJAX 请求提取的 tinyint(1) 数据视为 boolean 值?

eclipse - STS 无法使用 java 8 解析目标类型

java - 为什么 Stream.allMatch() 为空流返回 true?

javascript - ng-init 不适用于 sails.get ("/..")

JavaScript 相当于 C# 的 Windows.UI.Xaml.Application.OnActivated 事件?