Is there some way to wrap the entire page in a try/catch, so that I can catch any error from any script that is executing?
The try-catch statement should be used any time you want to hide errors from the user, or any time you want to produce custom errors for your users' benefit. If you haven't figured it out yet, when you execute a try-catch statement, the browser's usual error handling mechanism will be disabled.
JavaScript try and catchThe try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
For what it's worth, blanketing your code with try-catch statements is a pretty bad idea. If there's an error, you should resolve it, not allow it to happen but suppress it via try-catch. +1 for wanting to properly try catch all your functions.
You can nest one or more try statements. If an inner try statement does not have a catch -block, the enclosing try statement's catch -block is used instead. You can also use the try statement to handle JavaScript exceptions. See the JavaScript Guide for more information on JavaScript exceptions.
Use window.onerror
instead of a big try/catch
.
You could actually do some useful things in the error handler, like posting the error information to your server so you know when things are breaking on your page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With