Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are exceptions handled by the browser?

I decided to figure out how the browser processes the script with only one line in the code, and this line is an exception.

Let's say we have this exception:

throw Error("custom error");

By specification ecma262 ThrowStatement returns a completion record with throw type. When the script ScriptEvaluation is executed:

  1. If result.[[Type]] is normal, then
    • Set result to the result of evaluating scriptBody.

  1. Return Completion(result).

this makes it clear to us that a completion record with throw type is returned from ScriptEvaluation

But the ecma262 specification does not say when an error is thrown to the console. I need help to see how whatwg specification intercepts error throw from ecma262.

like image 927
MaximPro Avatar asked Mar 02 '26 03:03

MaximPro


1 Answers

The behaviour you're seeing is specified in the WHATWG HTML standard (emphasis mine).

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.

Also, ECMAScript doesn't actually have a console, that's specified at https://console.spec.whatwg.org

like image 109
snek Avatar answered Mar 04 '26 12:03

snek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!