JavaScript Try ... How to use the Catch declaration _ Basics

Source: Internet
Author: User
The role of Try...catch is to test errors in your code.
Instance
Try...catch statement
How to write a Try...catch declaration.
Try...catch declaration with a confirmation box
Another example of writing a try...catch statement. JavaScript-Catch Error
When we surf the web, we always see JavaScript warning boxes with runtime errors, and we ask "Do you want to debug?" ”。 An error message like this might be useful to the developer, not to the user. When errors occur, they tend to opt out of the site.
This section explains how to capture and process error messages for JavaScript so that you can provide more convenience to your audience.
There are two ways to catch errors in a Web page:
Use the Try...catch declaration. (Available in ie5+, Mozilla 1.0, and Netscape 6)
Use the OnError event. This is the old-fashioned way to catch errors. (after Netscape 3 version available)
Try ... Catch Declaration
Try...catch can test errors in your code. The try section contains the code that needs to be run, and the catch part contains the code that runs when the error occurs.
Grammar:
Try
{
Run code here
}
catch (Err)
{
Handling Errors Here
Note: Try...catch uses lowercase letters. Uppercase letters can go wrong.
Instance 1
The following example was originally used to display "Welcome guest!" when the user clicked on the button. The news. However, alert () in the message () function is mistakenly written as Adddlert (). Then the error occurred:
<ptml> <pead> <script type= "Text/javascript" > Function message () {Adddlert ("Welcome guest!") } </script> </pead> <body> <input type= "button" value= "View message" onclick= "message ()"/ > </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

We can add try...catch declarations so that we can take more appropriate action when the error occurs.
The following example modifies the script with the Try...catch declaration. The error occurred because of the mistake of writing alert (). This time, however, the catch section catches the error and uses a prepared code to handle the error. This code displays a custom error message to tell the user what happened.
<ptml> <pead> <script type= "Text/javascript" > var txt= "" function message () {try { Adddlert ("Welcome guest!") The catch (err) {txt= "There was a error on this page.\n\n" txt+= "error Description:" + Err.Description + "\ n \ n "txt+=" click OK to continue.\n\n "alert (TXT)}} </script> </pead> <body> <i Nput type= "button" value= "View message" onclick= "message ()"/> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Instance 2
The next example displays a confirmation box that lets the user choose to continue browsing the page by clicking the OK button when the error occurs, or by clicking the Cancel button to return to the homepage. If the return value of the Confirm method is false, the code redirects the user to another page. If the return value of the Confirm method is true, then the code does nothing.
<ptml> <pead> <script type= "Text/javascript" > var txt= "" function message () {try { Adddlert ("Welcome guest!") The catch (err) {txt= "There was a error on this page.\n\n" txt+= "click OK to continue viewing this page,\n" txt+= "or Cancel to" to "home page.\n\n" if (!confirm (TXT)) {document.location.href= http://www . w3school.com.cn/'}} </script> </pead> <body> <input type= button ' value= ' Vie W message "onclick=" message () "/> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
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.