gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:53:59 24 4
gpt4 key购买 nike

我决定用代码中的一行来弄清楚浏览器如何处理脚本,而这一行是一个异常(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/

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