TRY
Basic form
TRY.
Effect
You catch class-based exceptions in the statement block enclosed by the TRY and ENDTRY statements. To
catch these exceptions, you define handlers within the specified TRY block. You introduce a handler
using a CATCH statement, followed by the exception classes to be caught and the associated handler code.
After the CATCH clauses, you can declare a CLEANUP clause. The statements in the latter clause will
be executed if an exception is caught outsidethe TRY block. The structure of the TRY block is as follows:
TRY.
... guarded section
CATCH cx11 ... cx1n [INTO ex1].
... handlers for exceptions cx11 to cx1n
CATCH cx21 ... cx2m [INTO ex2].
... handlers for exceptions cx21 bis cx2m
... other handlers
CLEANUP.
... cleanup block
ENDTRY.
The TRY block is split into different sections using the CATCH and CLEANUP clauses.
-
Guarded section
The guarded section begins after the TRY statement and
ends at the first CATCH, CLEANUP,
or ENDTRY statement, depending on whether a handler has
been defined or not. Class-based exceptions that occur within the guarded section can be caught, and
the system can then react to them. Note that "deep" catching is also supported here, in contrast to
exceptions declared in an EXCEPTIONS addition or runtime errors
handled by CATCH SYSTEM-EXCEPTIONS. "Deep" catching allows you to catch exceptions that occur at a deeper level in the call hierarchy.
-
Handlers
You introduce a handler using a CATCH
clause, which specified the class-based exceptions to be caught. The CATCH clause is followed by the
handler code, which can contain any statements you want, and which ends with the next
CATCH, CLEANUP
or ENDTRY statement (depending on whether or not you have
defined other handlers).
If a (class-based) exception or catchable runtime error occurs within
the guarded section, the system looks for a suitable handler, starting from the first
CATCH clause, and the program flow continues from that point.
At the end of the handler, the system jumps to the ENDTRY
statement, and the program flow then continues from there. If the system cannot find a suitable handler,
it looks in each super-ordinate TRY block in the call
hierarchy. If it finds no handler at all, a runtime error occurs. Since the system looks for a handler
in the TRY block in strict order from top to bottom, you
must sort your CATCH clauses in ascending order - that
is, moving from the particular to the general exception classes.
You can only catch exceptions
that occur in the guarded section. In particular, this means that exceptions occurring within a handler
are not caught in the TRY block containing that handler.
However, the handler itself can contain a TRY block, which catches exceptions. Conversly, the exception can be caught in a super-ordinate context.
-
CLEANUP area
You introduce
the CLEANUP area in a CLEANUP
clause, followed by any statements you want. Declaring the CLEANUP
area is optional; however, it must be declared after the handlers, if it is declared at all. It ends
with an ENDTRY statement.
The CLEANUP area allows
you restore a consistent state. It is triggered if a (class-based) exception or catchable runtime error
occurs within the guarded section - and if they are to be caught at a higher level in the call hierarchy.
Using the CLEANUP area you can restore objects to a consistent
state or release global resources - even if, for example, a method has been interrupted.
Since
a CLEANUP area is only processed "along the way" when an exception is passed to its handler, the flow
of control within the CLEANUP block must not be changed
to the extent that the handler is no longer reached. Therefore, certain statements, which would change
the flow of control, are not permitted - such as RETURN,
STOP, REJECT,
CHECK, CONTINUE,
and
EXIT - if they would mean the system leaving the CLEANUP
block. Moreover, other (class-based) exception may influence the flow of control if they occur. For
this reason, such exceptions must be handled within the CLEANUP
block. You do this by encapsulating the statements that could potentially trigger these exceptions in
their own TRY block. The runtime system must leave the
CLEANUP block through the ENDTRY statement - otherwise, a runtime error occurs.
Related
CATCH,
CLEANUP, RAISE EXCEPTION
Additional help
Class-Based Exceptions