JavaScript Try...catch Exception Handling detailed

Source: Internet
Author: User
Tags error handling exception handling

Web page Special Try...catch exception handling detailed

Grammar

try{
Statement1
}catch (Exception) {
Statement2
}
It can be used to handle all or some of the errors that can occur in a script.
  
If a mistake is not caused by a try ... Catch the statement, it is through the error that other reports can handle.
  
If no other report handles errors, it is passed to the browser for processing.
  
Statement1 may occur where this is a mistake, and statement2 is used to handle errors.
  
When an error occurs, its value is passed to the catch drop Point declaration and stored in the exception.

<title>using a try. Catch statement</title>
<script language= "JavaScript" >
<!--
function MyErrorHandler (data) {
try{
try{
if (data = = "string") {
Throw "E0";
}else{
Throw "E1";
}
}catch (e) {
if (E = = "E0") {
Return ("error (" + E + "): entry must is numeric.");
}else{
Throw e;
}
}
}catch (e) {
Return ("error (" + E + "): entry is invalid.");
}
}
function ProcessData (form) {
if (isNaN (parseint (form.mytext.value)) {
Alert (MyErrorHandler ("string"));
}else{
Alert ("You have correctly entered a number");
}
}
-->
</script>
<body>
<form name= "MyForm" >
Please enter a number:
<input type=text size=10 value= "name=" MyText ">
<input Type=button value= "process" name= "mybutton" onclick= ' ProcessData (this.form) ' >
</form>
</body>

Error handling. Catch that error

<title>catch that error!</title>
<script>
function Catcherror (errstring) {
try {
try {
if (errstring = = 1)
throw new error ( -1, "errstring is-1!");
Else
throw new error (0, "errstring is not-1!");
}
catch (e) {
if (E.number = = 1)
Return (E.description + "got this one!");
Else
Throw e;
}
}
catch (e) {
Return (e.description + "This one is not handled here!");
}
}
</script>
<body>
<form name= "Theform" >
<input type=text name=errtext value= "-1" >
<input Type=button name=btnthrow value= "Catch it!" onclick= "alert (Catcherror (Document.theform.errtext.value));" >
</form>
</body>

Exception handling and attempts exception handling with Try/catch

<title></title>

<script language= " JavaScript "type=" Text/javascript
function Getmonthname (monthnumber) {
     throw " Invalidmonthnumber '
}
try {
    alert (getmonthname)
}
catch (Exception) {
& nbsp;   alert ("an" + Exception + "exception is encountered. " Contact the program vendor. ")

}

 

</script>
<body>

</body>

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.