Contents | Prev | Next | Index
When a section of code acquires a resource, it is often necessary to ensure that the resource be released again, regardless of whether the code completes normally or is interrupted by an exception. For example, a section of code that opens and processes a file will normally want to ensure that the file is closed no matter how the code terminates. The try-finally statement can be used in such situations.
try_finally_stmt ::= TRY stmt_seq FINALLY stmt_seq END |
A try-finally statement executes the statements in the try statement list in sequential order. If no exceptions are raised in the try statement list, the finally statement list is executed. If an exception is raised in the try statement list, control is transfered to the finally statement list, and once the finally statement list completes execution, the exception is re-raised. The resulting effect is that the finally statement list is always executed, regardless of how the try statement list terminates. Example:
Reset(F);
TRY
ProcessFile(F);
FINALLY
CloseFile(F);
END;
If a call to one of the standard procedures EXIT, BREAK, or CONTINUE causes control to leave the try statement list of a try-finally statement, the finally statement list is automatically executed. Likewise, if one of these standard procedures are used to leave an exception handler, the exeption object is automatically disposed.
Contents | Prev | Next | Index
Canterbury Pascal for JVM (Last documentation update
Sep 02, 2004)
Copyright © 1999-2004 J.Neuhoff - mhccorp.com
. All rights reserved.
Please send any comments or corrections to
neuhoff@mhccorp.com