Contents | Prev | Next | Index
Exceptions are handled using try-except statements. Its syntax is defined as follows:
try_except_stmt ::= try_except_part exception_block END try_except_part ::= TRY stmt_seq EXCEPT exception_block ::= exception_handlers default_handler ::= exception_handlers ::= stmt_seq ::= exception_handlers ; default_handler ::= exception_handlers ; exception_handlers ::= exception_handlers ; exception_handler ::= exception_handler exception_handler ::= ON exception_id DO stmt exception_id ::= <identifier> : class_id ::= class_id class_id ::= qualident default_handler ::= ELSE stmt_seq |
A TRY..EXCEPT executes the statements in the try statement list in sequential order. If the statements execute without any exceptions being raised, the exception block is being ignored, and control is passed to the statement the END keyword that ends the try-except statement.
The exception block in the EXCEPT..END section defines exception handlers for the try statement list. An exception handler can be invoked only by a raise statement executed in the try statement list or by a procedure or function or method called from the try statement list.
Raising an exception causes the control to be transfered to the innermost exception handler that can handle exceptions of a given class. The search for an exception handler starts with the most recently entered and not yet exited try-except statement. If that one cannot handle exceptions of the given class, the next most recently try-except statement is examined. This propagation of the exception to more outer levels continues until an appropriate handler is found, or until there are no more active try-except statements. In the latter case Pascal's default exception handler is executed, which in the case of runtime exceptions causes the program to be terminated.
To determined whether the exception block of a try-except statement can handle a particular exception, the ON..DO exception handlers are examined in order of appearance. The first exception handler whose defined exception class is assignment compatible with the thrown exception class. If an exception block contains an else-part, and if none of the ON..DO exception handlers match the exception, the else-part is considered a match. This compiler maps the else-part to Java's java.lang.Exception class to ensure it is a match with any other exception class. An exception block that contains only a statement list is considered a match for any exception class and, as in the else-part, is always mapped to Java's java.lang.Exception class.
Once a matching exception handler is found, the stack is unwound to the procedure or function or method that contains the handler, and control is transfered to the handler. The unwinding process will discard all procedure, function and method calls that occured since entering the try-except statement containing the exception handler. Following the excecution of an exception handler, the exception object is automatically destroyed, and control is passed to the statement following the END keyword that ends the try-except statement.
Example:
:
USES
java_lang_ArithmeticException,
java_lang_NumberFormatException,
java_lang_HandleRuntimeException;
:
TRY
Calculate;
ComputeValues;
EXCEPT
ON ArithmeticException DO
HandleArithmeticException;
ON NumberFormatException DO
HandleNumberFormatException;
ELSE
HandleRuntimeException;
END;
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