Using The TRY & CATCH Statement To Catch Errors

 

This script uses the Try & Catch statements to catch any JavaScript errors, which may have occurred on a page.

In the example below, I use an error in the message function. As you can see below I have inserted the following:

notalert("Welcome to www.wizzardweb.co.uk!")

What I should have done is:

alert("Welcome to www.wizzardweb.co.uk!")

Thus the error message is displayed using the Try & Catch statements.

If you decide you want to use or alter the script to suit your needs, please give your support and leave a link to this site, thanks.

 

 

Example

 

 

 

Script Source

 

<script type="text/javascript">
var errcatch=""
function message()
{
try
{
notalert("Welcome to www.wizzardweb.co.uk!")
}
catch(err)
{
txt="SORRY! There was an error on this page.\n\n"
txt+="**** " + err + " ****\n\n"
txt+="Click OK to report the error or CANCEL to continue.\n\n"
if(confirm(txt))
{
alert('This would be the report page, which you would send the user to')
}
}
}
// Script by www.wizzardweb.co.uk
</script>

 

Please let me know if this script was helpful to you.

[rating]