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