JS Getting Started Tutorial: JavaScript Try Catch Error Capture Instance tutorial

Source: Internet
Author: User
Tags try catch

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.