Write a Global Error Handler in JavaScript



The following global error handler will show how to catch unhandled exception −

Example

<!DOCTYPE html>
<html>
   <body>
      <script>
         window.onerror = function(errMsg, url, line, column, error) {
            var result = !column ? '' : '
column: ' + column;             result += !error;             document.write("Error= " + errMsg + "
url= " + url + "
line= " + line + result);             var suppressErrorAlert = true;             return suppressErrorAlert;          };          setTimeout(function() {             eval("{");          }, 500)       </script>    </body> </html>
Updated on: 2020-06-16T11:40:30+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements