JavaScript Try catch is used in JS to catch errors, let's look at the examples and tutorials for Try catch.
One of the try ... catch declarations allows you to test the error of a block of code.
Example
One of the try ... catch declarations
How to compose a try ... catch statement.
<script type= "Text/javascript" >
var txt= "";
Function message ()
{
Try
{
Adddlert ("Welcome guest!");
}
catch (Err)
{
Txt= "There was a error on this page.nn";
txt+= "Error Description:" + err.description + "NN";
txt+= "Click OK to Continue.nn";
alert (TXT);
}
}
</script>
<body>
<input type= "button" value= "View message" onclick= "message ()"/>
</body>
One of the try ... catch Declaration's confirmation box
Another example is how to write a try ... catch statement.
<script type= "Text/javascript"
var txt= ""
Function message ()
{
Try
{
; Adddlert ("Welcome guest!");
}
catch (Err)
{
txt= "There was a error on this page.nn";
txt+= ' Click OK to continue VI Ewing this Page,n ";
txt+= "or Cancel to return to the home page.nn";
if (!confirm (TXT)
{
document.location.href= http:// www.111cn.net/";
}
}
}
</script>
<body>
<input type= "button" value= "View message" onclick= "message ()"/>
</body>
JavaScript-catch Error
When browsing the web on the Internet, we have seen a JavaScript warning box telling us that there is a run-time error and asking: "Do you want to debug?" ” 。 Error messages This may be useful development rather than a user. When users see errors, they tend to leave the page.
This chapter will teach you how to capture and handle JavaScript error messages, so don't lose your audience.
There are two ways to catch up on an error page in:
by using the Try ... catch statement (for IE5 +, Mozilla 1.0 and Netscape 6)
by using the OnError event. This is an old standard solution that found errors (www.111cn.net )
attempt ... catch statement
The TRY ... catch declaration allows you to test a block of code for errors. The try block contains code that runs and the Catch block contains code that will be executed if an error occurs.
Syntax