September 13, 2002 - Processing in the try, catch, and finally Blocks | WebReference

September 13, 2002 - Processing in the try, catch, and finally Blocks

Yehuda Shiran September 13, 2002
Processing in the try, catch, and finally Blocks
Tips: September 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

The try...catch...finally statement in JScript .NET lets you handle the errors that may occur in the try block. If an error occurs in the try block, program control is passed to the catch block. The value passed to the catch block as a parameter is the value of the error that occurred in the try block. If no error occurs, the catch block is not executed. For errors that you don't handle in the catch block, JScript .NET provides its normal error message to the user, as if there was no error handling. But remember, avoid showing system errors to the user, as they may give the impression of buggy and sloppy coding.

After all statements in the try block have been executed and any error processing has been completed in the catch block, the finally block is unconditionally executed. The code inside the finally block is always executed. Even if the catch block re-throws the error or if a return statement occurs inside the catch or try blocks, the finally block is guaranteed to run. The only exception is when an unhandled error occurs; in this case, the system processes this run-time error by printing a message to the user and quitting from the JScript .NET application.

To learn more about exception handling, go to Column 118, JScript .NET, Part XII: Exception Handling.