try...catch...finally statement

Source: Internet
Author: User
Tags contains error handling exception handling
Statement

Implement error handling for JScript.

try {
tryStatements}
catch(exception){
catchStatements}
finally {
  finallyStatements}

Parameters

Trystatement

Required option. The statement that the error may occur.

exception

Options available. Any variable name. The initialization value of the exception is the value of the error thrown out.

Catchstatement

Options available. A statement that handles errors that occur in the associated trystatement .

Finallystatements

Options available. A statement that executes unconditionally after all other processes occur.

Description

The try...catch...finally statement provides a way to handle some or all of the errors that can occur in a given block of code while still keeping the code running. If there is an error that the programmer has not handled, JScript only gives the user its normal error message, as if there were no error handling.

The trystatements parameter contains code that may have an error, and catchstatement contains code to handle any errors that occurred. If an error occurs in the trystatements , the program control is passed to catchstatements for processing. The initialization value of the exception is the value of the error that occurred in trystatements . If the error does not occur, the catchstatementsis not executed.

If the error is not handled in the catchstatements associated with the trystatements in which the error occurred, the throw statement is used to propagate, Or throw this error back to the more advanced error handler.

After executing the statement in trystatements , and after all error handling of catchstatements , unconditional execution can be performed finallystatements The statement in.

Note that even if you return a statement in a try or catch block, or if you throw an error back in the catch block, the finallystatements encoding is still performed. It is generally ensured that the finallystatments is running unless an unhandled error exists. (for example, a run-time error occurs in a catch block.) )。

Example

The following example illustrates how JScript exception handling is done.

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 {  Make the amendment so as to draw WScript.Echo(s)function print(s){   document.write(s);}

The following results will be drawn:

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

Requirements

Version 5

Please see

Throw statement



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.