SAP CATCH TRY ABAP Statements

Get Example source ABAP code based on a different SAP table
  


Standard SAP Help for CATCH_EXCEPTION

CATCH

Short Reference
• CATCH ABAP Statement


ABAP Syntax CATCH [BEFORE UNWIND] cx_class1 cx_class2 ...
[INTO oref].

ABAP Addition
1 ... BEFORE UNWIND
2 ... INTO oref

What does it do? Introduction of a CATCH block of a TRY control structure in which exceptions can be handled.

A CATCH block is an exception handler, meaning the program logic that is executed whenever the associated exception is raised in the TRY block of the same TRY control structure.

A CATCH block handles the exceptions of the exception classes cx_class1 cx_class2 ... that are specified after the statement CATCH as well as the exceptions of the subclasses of these exception classes. In each CATCH statement of a TRY control structure, you can list any number of exception classes cx_class1 cx_class2 ..., whereby more special exception classes (subclasses) must be listed before more general exception classes (superclasses). This order must be kept both within a CATCH statement and across multiple CATCH statements of a TRY control structure.



Latest notes:The rule whereby CATCH special exception classes
must be listed before general classes ensures that an exception is not handled by a general exception handler (superclass) if a special handler (subclass) is provided.
• BEFORE UNWIND CATCH

ABAP Addition

What does it do? If the addition BEFORE UNWIND is specified, the context in which the exception was raised, including all called procedures and their local data, is deleted only after exiting the CATCH block. If the addition is not specified, the context is deleted before the CATCH block is executed.

Latest notes:If the addition BEFORE UNWIND is specified, the statement RESUME can be used in the CATCH block for handling a resumable exception , to resume processing after the statement that raised the exception. This is the only case in which the context of the exception is not deleted when the CATCH block is exited.
Resumable exceptions can also be handled in CATCH blocks without the addition BEFORE UNWIND. In this case, the context of the exception is deleted before the handling process and the statement RESUME cannot be specified.
Any CLEANUP blocks are always executed directly before their context is deleted. If BEFORE UNWIND is used, after exception handling, and in all other cases before the exception handling.
Use of the addition BEFORE UNWIND for CATCH is only required when you use the statement RESUME. However, it is allowed in principle during exception handling if the context of the exception is to be evaluated before any cleanup activities in CLEANUP blocks. This makes sense, for example, when handling resource bottlenecks if releasing resources in CLEANUP blocks would change the context and thus make the calculation of the free resources in the exception handler meaningless. Other than for logging purposes, we do not recommend evaluating the part of the context that is only of interest locally for implementing the incorrect procedure.
• INTO CATCH

ABAP Addition

What does it do? If the addition INTO is specified, a reference to the exception object is saved to oref. The following can be specified for oref: An existing object reference variable oref, whose static type must be more general or as general as the most general of the specified exception classes. An inline declaration with DATA(var). The static type of the declared object reference variable is the exception class (if specified). If multiple exception classes are specified and a common superclass of these classes is used, the superclass is the static type of oref; if not, it is CX_ROOT .

The object reference variable can be used to access the attributes and methods of the exception object.



Example ABAP Coding
Catches exceptions with an inline declaration of an
object reference variable. The static type of this variable is cx . CLASS cx DEFINITION INHERITING FROM cx_dynamic_check.
PUBLIC SECTION.
ENDCLASS.

CLASS cy DEFINITION INHERITING FROM cx.
PUBLIC SECTION.
ENDCLASS.

TRY.
...
CATCH cy cx INTO DATA(oref).
ENDTRY.

Return to menu