html - 浏览器如何处理异常?

标签 html ecmascript-6

我决定用代码中的一行来弄清楚浏览器如何处理脚本,而这一行是一个异常(exception)。

假设我们有这个异常(exception):

throw Error("custom error");

按规范 ecma262 ThrowStatement返回抛出类型的完成记录。 当脚本 ScriptEvaluation执行:

  1. If result.[[Type]] is normal, then
    • Set result to the result of evaluating scriptBody.
<小时/>
  1. Return Completion(result).

这让我们清楚,一条抛出类型的完成记录是从 ScriptEvaluation 返回的。

但是 ecma262 规范并没有说明何时向控制台抛出错误。我需要帮助来了解 Whatwg 规范如何拦截 ecma262 抛出的错误。

最佳答案

您所看到的行为是在 WHATWG HTML 标准中指定的(重点是我的)。

https://html.spec.whatwg.org/multipage/webappapis.html#calling-scripts

  1. Otherwise, set evaluationStatus to ScriptEvaluation(script's record). If ScriptEvaluation does not complete because the user agent has aborted the running script, leave evaluationStatus as null.

  2. If evaluationStatus is an abrupt completion, then:

    1. ...

    2. ...

    3. Otherwise, rethrow errors is false. Perform the following steps:

      1. Report the exception given by evaluationStatus.[[Value]] for script.

      2. Clean up after running script with settings.

      3. Return evaluationStatus.

https://html.spec.whatwg.org/multipage/webappapis.html#runtime-script-errors-in-documents

When the user agent is to report an exception E, the user agent must report the error for the relevant script, with the problematic position (line number and column number) in the resource containing the script, using the global object specified by the script's settings object as the target. If the error is still not handled after this, then the error may be reported to a developer console.

此外,ECMAScript 实际上没有控制台,在 https://console.spec.whatwg.org 中指定。

关于html - 浏览器如何处理异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59031259/

相关文章:

html - 使用 popover api 和 `<div role="按钮">`作为调用元素

jquery - 允许出现在任何地方的 HTML5 元素

javascript - 如何删除对象键的空格? [for...in] [keys.forEach] [reduce]

javascript - 关于在浏览器中支持 JavaScript 模块(原生的,没有 babel 或 require.js)

html - 页眉中的递增数字导致页面滚动时出现微卡顿

php - 谷歌地图地理定位不请求许可

javascript - PreventDefault() 不适用于特定选项选择

syntax-error - native (ES5)至(ES6)

javascript - 我在哪里可以找到 es5、es6 和 es7 的完整功能列表?

javascript - React Native 将组件/对象属性传递给 OnPress 函数