ABAP Keyword Documentation → ABAP − Reference → Program Flow Logic → Exception Handling → Class-Based Exceptions
TRY
Other versions: 7.31 | 7.40 | 7.54
Syntax
TRY.
[try_block]
[CATCH [BEFORE UNWIND] cx_class1 cx_class2 ... [INTO oref].
[catch_block]]
...
[
CLEANUP [INTO oref].
[cleanup_block]]
ENDTRY.
Effect
The statement TRY introduces a control structure with multiple statement
blocks. The first statement block try_block is always executed, whereas a branching off to exactly one of the remaining statement blocks only occurs if a
class-based exception is raised in try_block.
A TRY control structure defines the following statement blocks:
-
A
TRYblocktry_blockdirectly after the statementTRY. TheTRYblock defines a protected area whose class-based exceptions can be handled in the subsequentCATCHblocks. If no exception is raised in theTRYblock and it reaches its end, the system resumes processing afterENDTRY. If a class-based exception is raised in theTRYblock, the system searches for an exception handler in the sameTRYcontrol structure or in an external structure (see System Behavior). -
One or more optional
CATCHblockscatch_blockfor handling exceptions, each directly after aCATCHstatement. If the end of aCATCHblock is reached without it being left early, the processing continues after theENDTRY. The special statementsRETRYandRESUMEcan be listed in aCATCHblock, to control the behavior of exception handling. -
An optional
CLEANUPblockcleanup_blockfor cleanups directly after theCLEANUPstatement.
A TRY control structure invalidates the simultaneous use of the obsolete statement CATCH SYSTEM-EXCEPTIONS to handle
catchable runtime errors in the current
processing block.
Notes
-
All statement blocks of a
TRYcontrol structure can contain any kind of control structures, in particular furtherTRYcontrol structures. -
No exceptions (except those in category CX_NO_CHECK from event handlers) can be propagated from the
static constructors and
event handlers, which means they must always be handled locally.
Example
Division by zero in a TRY block. The relevant exception is caught with CATCH.
TRY.
cl_demo_output=>display( 1 / 0 ).
CATCH cx_sy_arithmetic_error INTO DATA(exc).
cl_demo_output=>display( exc->get_text( ) ).
ENDTRY.