Friday, May 17, 2024
168
rated 0 times [  174] [ 6]  / answers: 1 / hits: 23810  / 11 Years ago, thu, december 12, 2013, 12:00:00

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?


More From » error-handling

 Answers
13

In the browser define the window.onerror function. In node attached to the uncaughtException event with process.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



window.onerror = function(error) {
// do something clever here
alert(error); // do NOT do this for real!
};


Node.js



process.on('uncaughtException', function(error) {
// do something clever here
alert(error); // do NOT do this for real!
});

[#73776] Wednesday, December 11, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mustafaericho

Total Points: 322
Total Questions: 103
Total Answers: 110

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
mustafaericho questions
Mon, May 31, 21, 00:00, 3 Years ago
Sun, May 23, 21, 00:00, 3 Years ago
Sat, Feb 13, 21, 00:00, 3 Years ago
Sat, Jan 2, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
;