So I’m trying to wrap my head around something I just read in “professional Javascript”, by Nicholas Zakas: It gives the following code as an example:
window.onerror = function(message, url, line){
alert(message);
return false;
};
and then says: “By returning false, this function effectively becomes a try-catch statement for the entire document, capturing all unhandled runtime errors. This event handler is the last line of defense against errors being reported by the browser..”
So my question is, will this piece of code prevent the execution of your script from stopping, with any error it encounters? or are there any errors that will still stop execution? thanks.
edit: maybe a better question would be, why is this not a good method for preventing a script from stopping its execution when it hits an error..
I’m fairly sure something like a syntax error would be considered fatal whether or not the exception is ignored.
Using this is a bad idea, partly because there’s already a perfectly adequate exception handling mechanism in the browser’s error console, but mostly because if your site has errors then blasting them in your users’ faces will not make them likely to return.