JS Using onerror to catch an exception example

  • 2021-07-07 06:23:12
  • OfStack

This article illustrates how JS uses onerror to catch exceptions. Share it for your reference, as follows:

If you need to take advantage of onerror events, you must create a function to handle errors. You can call this onerror event handler (onerror event handler). This event handler is called with three parameters: msg (error message), url (url of the page where the error occurred), and line (line of code where the error occurred).


<head>
<script type="text/javascript">
onerror=handleErr
var txt=""
function handleErr(msg,url,l)
{
txt="There was an error on this page.\n\n"
txt+="Error: " + msg + "\n"
txt+="URL: " + url + "\n"
txt+="Line: " + l + "\n\n"
txt+="Click OK to continue.\n\n"
alert(txt)
return true
}
function message()
{
adddlert("Welcome guest!")
}
</script>
</head>
<body>
<input type="button" value="View message" onclick="message()" />
</body>
</html>

For more readers interested in JavaScript related contents, please check the topics of this site: "Summary of JavaScript Errors and Debugging Skills", "Summary of JavaScript Mathematical Operation Usage", "Summary of json Operation Skills in JavaScript", "Summary of JavaScript Switching Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Effects and Skills", "Summary of JavaScript Data Structure and Algorithm Skills" and "Summary of JavaScript Traversal Algorithm and Skills"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: