admin管理员组

文章数量:1312792

I recently had an exception occurring that I didn't notice till I looked at chrome dev tools console.

Does a website's javascript just handle and log exceptions at a higher level somewhere? I didn't handle the exception in function1 below, so the later functions were just not being run or something.

what exactly happens when an exception happens? I'm used to an unhandled exception meaning the program just stops

containingFunction
{
   function1();
   function2()
   function3()
   return;
}

I recently had an exception occurring that I didn't notice till I looked at chrome dev tools console.

Does a website's javascript just handle and log exceptions at a higher level somewhere? I didn't handle the exception in function1 below, so the later functions were just not being run or something.

what exactly happens when an exception happens? I'm used to an unhandled exception meaning the program just stops

containingFunction
{
   function1();
   function2()
   function3()
   return;
}
Share Improve this question asked May 23, 2019 at 5:20 James Joshua StreetJames Joshua Street 3,41911 gold badges50 silver badges87 bronze badges 1
  • 4 Unhandled exception means that the current execution just stops. Whether or that affects the entire website will depend on how the application is structured and probably where the exception occurs. – VLAZ Commented May 23, 2019 at 5:22
Add a ment  | 

2 Answers 2

Reset to default 9

TLDR

If an exception is not caught and is allowed to bubble up to global scope then the javascript engine will simply stop processing anything.

So it is not simply:

later functions were just not being run or something

It is any code below the exception will not be run. Regardless of whether they are functions, if statements, loops etc.

But.. events keep happening??

Well.. it depends.

To be more specific, the browser stops the current execution of scripts (1) and enter the event loop. This means that any previous code that have managed to schedule events in the future before the exception will get that future callback executed.

For example, the following code:

<html>
<head>
  <script>
    setTimeout(() => console.log("ha"),1000);

    throw new Error("oops");

    console.log("ho");
  </script>
</head>
<body>
</body>
</html>

Or if you are more used to looking at promise based code:

<html>
<head>
  <script>
    Promise.resolve().then(()=>console.log("ha"));

    throw new Error("oops");

    console.log("ho");
  </script>
</head>
<body>
</body>
</html>

Will both throw an error and print "ha" but will not print "ho".

But this is dependent on the interpreter. The above is true for browsers but not node.js. Node simply exits the process on any uncaught errors.

For example the code:

setTimeout(() => console.log("ha"),1000);

throw new Error("oops");

console.log("ho");

Or if you are more used to looking at promise based code:

Promise.resolve().then(()=>console.log("ha"));

throw new Error("oops");

console.log("ho");

Will not print "ha" nor "ho" when executed with node.

(1) - By "scripts" I mean the script you wrote yourself as a web developer. Of course I don't mean any of the code that the browser developers wrote.

When you construct your page you can place the JavaScript in the header tag or at the bottom of the page (or anywhere in between for that matter). Some browsers execute the JavaScript only after the HTML and CSS is loaded, and some runs the JavaScript as it is being loaded.

A lot of people's html layout looks like this:

<html>
<header>
    <title></title>

    <!--Styles-->
    <!--End Styles-->
</header>
<body>
    <!--All content-->

    <!--End All content-->

    <!--Scripts-->
    <!--End Scripts-->
</body>

Reason being that the JavaScript doesn't delay the page load speed and if it breaks, most of the page is already loaded/rendered. When JavaScript breaks without a try { } catch(err) { }, the rest of it won't be executed but it doesn't throw errors on the page hence why the console exists in the developer tools (most browsers has this functionality)

Read more here on w3schools.

JavaScript try catch:

try {
    //Do your work here
} catch(err) {
    console.log(err.message);
}

本文标签: what happens to unhandled exceptions in a website39s javascriptStack Overflow