JavaScript Try Catch Instance Tutorial

Source: Internet
Author: User
Tags try catch

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

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.