How to write a global error handler in JavaScript?

JavascriptWeb DevelopmentFront End Technology

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 ? '' : '\ncolumn: ' + column;
            result += !error;
            document.write("Error= " + errMsg + "\nurl= " + url + "\nline= " + line + result);
            var suppressErrorAlert = true;
            return suppressErrorAlert;
         };
         setTimeout(function() {
            eval("{");
         }, 500)
      </script>
   </body>
</html>
raja
Published on 30-Jan-2018 16:25:06
Advertisements