SAP CATCH TRY ABAP Statements



Get Example source ABAP code based on a different SAP table
  


ID CATCH
• CATCH ABAP Statement

CATCH
Short Reference

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

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

What does it do?
Introduces 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, a list of any number of exception classes cx_class1 cx_class2 ... can be specified, with more specific exception classes (subclasses) having to be specified before more general exception classes (superclasses). This order must be followed both within a CATCH statement and across multiple CATCH statements of a TRY control structure.
Before the CATCH block is executed, the system by default deletes the context in which the exception was raised. To get the context during execution of the CATCH block, the BEFORE UNWIND addition can be used.



Latest notes:

The rule that special exception classes must be specified before general exception classes in CATCH ensures that an exception is not handled by a general exception handler for a superclass if a special handler for a subclass is provided.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Catch of two possible exceptions with CATCH. If the input cannot be interpreted as number, the exception CX_SY_CONVERSION_NO_NUMBER is caught by its superclass CX_SY_CONVERSION_ERROR. If the number 0 is entered, the exception CX_SY_CONVERSION_ERROR is caught by its superclass CX_SY_ARITHMETIC_ERROR.
ABEXA 00091
ABAP_EXAMPLE_END

ABAP_EXAMPLE_ABEXA
Exceptions, CATCH
ABAP_EXAMPLE_END
• BEFORE UNWIND CATCH

ABAP Addition

What does it do?
If the addition BEFORE UNWIND is specified, the context in which the exception was raised is not deleted until the CATCH block is executed. Instead, the context, including all called procedures and their local data, is preserved during the execution of the CATCH block.
If no RESUME statement is executed in the CATCH block, the context is deleted when the CATCH block is exited.
If a RESUME statement is executed in the CATCH block, processing resumes after the place that raised the exception.
Any CLEANUP blocks are always executed directly before their context is deleted. This means that when BEFORE UNWIND is used, after exception handling, and in all other cases before the exception handling. In a CATCH block with BEFORE UNWIND, no statements can be executed in which the context is deleted without executing any CLEANUP blocks.
The statements CALL TRANSACTION, CALL DIALOG, SUBMIT, STOP, REJECT, CHECK SELECT-OPTIONS , and the obsolete variant of LEAVE are forbidden by the syntax, as they might leave the ABAP_ISESS .
Statements such as LEAVE TO TRANSACTION, which are statically known to leave the ABAP_ISESS , are allowed. In such cases, exception handling is properly terminated while executing the CLEANUP blocks before the statement is executed.
Procedure calls are allowed. However, if the context is deleted during such a procedure call, a runtime error EXCP_HANDLER_FAILED_TO_UNWIND occurs. This is also the case if a statement is executed here that is allowed directly in the CATCH block. For example, LEAVE TO TRANSACTION can be executed directly in the CATCH block, but not in a procedure that is called there, because otherwise the context would be deleted without executing the CLEANUP blocks.
If the message type is dynamically specified for the MESSAGE statement for sending messages, the ABAP runtime framework behaves as if exception handling is to be exited. The context is deleted during the execution of the CLEANUP blocks. However, if the system returns to the CATCH block after the MESSAGE statement has been executed, which can be the case with message types I and S, for example, the exception CX_SY_ILLEGAL_HANDLER is raised.
When combined with INTO, the addition BEFORE UNWIND sets the attribute IS_RESUMABLE of the exception object and the preceding exception objects of a chaining with the attribute PREVIOUS. Up until the first resumable raised exception, IS_RESUMABLE is set to the value of abap_true and is otherwise set to the value of ABAP_FALSE.



Latest notes:

If addition BEFORE UNWIND is not specified, the context is deleted before the CATCH block is executed.
The statement RESUME can be used only when handling a resumable exception and only in a CATCH block for which the addition BEFORE UNWIND is specified. This is the only case where 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.
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 is useful , 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, evaluating the part of the context that is only of interest locally for implementing the incorrect procedure is not recommended.
In a procedure called up using BEFORE UNWIND during a CATCH block, each MESSAGE statement that sends a message causes the runtime error EXCP_HANDLER_FAILED_TO_UNWIND.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Use of the addition BEFORE UNWIND when a resumable exception cx_demo is caught.