I know this has probably been asked before, but I can't find where:
I know you can detect JS errors using extensions in stuff, but is there any way to detect ALL errors using JavaScript and display an alert whenever there is one?
I know this has probably been asked before, but I can't find where:
I know you can detect JS errors using extensions in stuff, but is there any way to detect ALL errors using JavaScript and display an alert whenever there is one?
In the browser define the
window.onerror
function. In node attached to theuncaughtException
event withprocess.on()
.This should ONLY be used if your need to trap all errors, such as in a spec runner or console.log/ debugging implementation. Otherwise, you will find yourself in a world of hurt trying to track down strange behaviour. Like several have suggested, in normal day to day code a
try / catch
block is the proper and best way to handle errors/exceptions.For reference in the former case, see this (about window.error in browsers) and this (about uncaughtException in node). Examples:
Browser
Node.js