September 13, 2002 - Processing in the try, catch, and finally Blocks
September 13, 2002|
Processing in the try, catch, and finally Blocks
Tips: September 2002
Yehuda Shiran, Ph.D.
try...catch...finallystatement in JScript .NET lets you handle the errors that may occur in the
tryblock. If an error occurs in the
tryblock, program control is passed to the
catchblock. The value passed to the
catchblock as a parameter is the value of the error that occurred in the
tryblock. If no error occurs, the
catchblock is not executed. For errors that you don't handle in the
catchblock, 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
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.