JS Getting Started Tutorial: JavaScript Try ... Catch Error Capture Instance tutorial
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>
The confirmation box for the try ... catch declaration
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 viewing this page,n";
txt+= "or Cancel to return to the home page.nn";
if (!confirm (TXT))
{
Document.location.href= "http://111cn.net/";
}
}
}
</script>
<body>
<input type= "button" value= "View message" onclick= "message ()"/>
</body>
Javascript-catching Errors
When browsing the web on the Internet, we've all seen a JavaScript warning box that tells us there is a run-time error and asks: "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 (applies to IE5 +, Mozilla 1.0 and Netscape 6)
By using the OnError event. This is the old standard solution found error (available from Netscape Company 3)
Try ... catch statement
One of the try ... catch declarations allows you to test the error of a block of code. The try block contains code that runs and the Catch block contains code that will be executed if an error occurs.
Grammar
Try
{
Run some code here
}
catch (Err)
{
Handle Errors Here
}
Please note that try ... Catch writes letters. A JavaScript error is generated in uppercase letters! Example 1 The following example contains a script that is to display the message "Welcome guests!" "When you press a button." However, there is a typo in the message () function. Warning () is the wrong Adddlert (). JavaScript error occurred: <script type= "Text/javascript" >
Function message ()
{
Adddlert ("Welcome guest!");
}
</script>
<body>
<input type= "button" value= "View message" onclick= "message ()"/>
</body>