en.javascript.info icon indicating copy to clipboard operation
en.javascript.info copied to clipboard

Wrong code in error handling chapter "Error handling, try catch"

Open tausiq2003 opened this issue 2 years ago • 2 comments

There is a wrong code in the Error Handling chapter. Refer to the image and code below:

try {
  user = { /*...*/ };
} catch (err) {
  if (err instanceof ReferenceError) {
    alert('ReferenceError'); // "ReferenceError" for accessing an undefined variable
  }
}

This should output as follows in a browser environment. image

and, nothing should be there in the terminal in Node js env. as nothing is being returned or logged. But, as per the website when you ran the code, it shows this:

image

For reference it's the second code snippet under: https://javascript.info/try-catch#rethrowing

It might be using "use strict" internally, but its quite misleading.

tausiq2003 avatar Oct 31 '23 18:10 tausiq2003

From "The modern mode, 'use strict'" chapter:

image

Alexandre887 avatar Nov 04 '23 00:11 Alexandre887

@Alexandre887 Ok understood, so I guess its a good practice to use the following, what do you think? "use strict" window.onerror() process.on() in Node env.

tausiq2003 avatar Nov 04 '23 03:11 tausiq2003