How to catch all JavaScript unhandled exceptions?

To catch all JavaScript unhandled exceptions, use window.error. The onerror event handler provides three pieces of information to identify the exact nature of the error −

  • Error message − The same message that the browser would display for the given error
  • URL − The file in which the error occurred
  • Line number− The line number in the given URL that caused the error


You can try to run the following code to catch unhandled exceptions −

            window.onerror = function (msg, url, line) {
               alert("Message : " + msg );
               alert("url : " + url );
               alert("Line number : " + line );

      <p>Click the following to see the result:</p>
         <input type = "button" value = "Click Me" onclick = "myFunc();" />

Updated on: 23-Jun-2020


Kickstart Your Career

Get certified by completing the course

Get Started