Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Program Flow Logic →  Exception Handling →  Class-Based Exceptions →  TRY 

CATCH

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


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

Extras

1. ... BEFORE UNWIND
2. ... INTO oref

Effect

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, any number of exception classes cx_class1 cx_class2 ... can be specified, with more specific exception classes (subclasses) being 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.


Note

The rule where 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.


Example

See Exceptions, CATCH.

Addition 1

... BEFORE UNWIND

Effect

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.


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 the statement RESUME is used. 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.

Addition 2

... INTO oref

Effect

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

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.