Use of try catch finally in JavaScript (reprinted)

Source: Internet
Author: User
Use of try catch finally in Javascript

Try... catch... finally statement

Implements error handling for JScript.
Try {
Trystatements}
Catch (exception ){
Catchstatements}
Finally {
Finallystatements}
==================
Parameters
Trystatement
Required. An error may occur.
Exception
Required. Any variable name. The initialization value of exception is the thrown error value.
Catchstatement
Optional. The statement that handles errors in the associated trystatement.
Finallystatements
Optional. The statements that are unconditionally executed after all other processes occur.
Description
The try... catch... finally statement provides a way to handle the problem that may occur in a given Code Some or all errors in the block, while still keeping the code running. If an error is not handled by a programmer, JScript only provides the user with its common error information, as if there is no error processing.

The trystatements parameter contains code that may cause errors, while the catchstatement parameter contains code that handles any errors. If an error occurs in trystatements Program Control is passed to catchstatements for processing. The initialization value of exception is the error value in trystatements. If the error does not occur, no Run Catchstatements.

If the error cannot be handled in the catchstatements associated with the trystatements with which the error occurs, use the throw statement to spread the error or throw the error again Advanced .

After you have executed the statements in trystatements and processed all errors in catchstatements, You can unconditionally execute the statements in finallystatements.

Please note that the finallystatements code will be executed even if a statement is returned in the try or catch block or an error is thrown again in the Catch Block. The finallystatments Run , Unless there are unprocessed errors. (For example, a runtime error occurs in the Catch Block .).

 

Example
The following example illustrates how JScript special case processing is performed.

Try {
Print ("outer try running ..");
Try {
Print ("nested try running ...");
Throw "an error ";
}
Catch (e ){
Print ("nested catch caught" + E );
Throw e + "re-thrown ";
}
Finally {
Print ("nested finally is running ...");
}
}
Catch (e ){
Print ("outer catch caught" + E );
}
Finally {
Print ("outer finally running ");
}
// Modify the Windows Script Host to obtain wscript. Echo (s)
Function print (s ){
Document. Write (s );
}
The following results are obtained:

Outer try running ..
Nested try running...
Nested catch caught an error
Nested finally is running...
Outer catch caught an error re-thrown
Outer finally running

 

 

 

The following is an example of JavaScript exception handling.

VaR array = NULL;
Try {
Document. Write (array [0]);
} Catch (ERR ){
Document. writeln ("error name:" + err. Name + "");
Document. writeln ("error message:" + err. Message );
}
Finally {
Alert ("object is null ");
}

Program Execution Process

1. When array [0] is created, array is an empty object because array [0] is not created. If array [0] is called in the program, the object is null.
2. The catch (ERR) statement captures this exception and prints the Error Type through err. Name, and err. Message prints the error details.
3. Finally is similar to Java finally, and will be executed no matter whether exceptions exist.

We will summarize the information corresponding to the six values of error. Name:
1. evalerror: eval () is used and defined differently.
2. rangeerror: value out of bounds
3. referenceerror: Invalid or unrecognized reference value
4. syntaxerror: Syntax Parsing Error
5. typeerror: Incorrect operand type
6. urierror: The URI processing function is improperly used.

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.