SAP CLASS EXCEPTION - Guide
Get Example source ABAP code based on a different SAP table
Classic and Class-Based Exceptions
ABAP_BACKGROUND
For reasons of downward compatibility, there are two options for defining standalone catchable exceptions in ABAP:
The coexistence of the two exception concepts is regulated as follows:
ABAP_RULE
Only raise class-based exceptions in new procedures if it is possible to dispense with classic exceptions from the technical point of view.
ABAP_DETAILS
User-defined classic exceptions are little more than return codes. If a classic exception is raised in a procedure using the statement
The raising of class-based exceptions, however, results in a change of the program flow. They can either be handled directly or propagated upwards along the call hierarchy. In this way, not every procedure (
By default, raising an exception stops the entire current context even if the exception is handled. However, there may be situations (mass data processing, for instance) in which a single error does not justify canceling an entire service. For these cases, class-based exceptions can be raised and propagated as resumable exceptions (
Class-based exceptions completely replace the classic exceptions for new code (of course, there are exceptions to this rule) and add resumability. Although classic exceptions on the raiser side are completely obsolete from a technical point of view, the following must be considered for older code: Even if the raiser side is under control, it is not simply a case of switching older procedures over to class-based exceptions, because then all usage occurrences would have to be modified.
When existing procedures that use classic exceptions are called, they must continue to be handled in the new code. In this case, we recommend mapping the classic exceptions to equivalent class-based exceptions by using
Exception
Since class-based exceptions are currently not supported in remote-enabled function modules (RFM), classic exceptions still need to be implemented and handled for remote function calls (RFCs).
ABAP_EXAMPLE_BAD
The following source code shows the declaration and the raising of a classic exception in a method as well as their handling by evaluating
PUBLIC SECTION.
METHODS do_something
EXCEPTIONS application_error.
ENDCLASS.>
METHOD do_something.
...
RAISE application_error.
...
ENDMETHOD.
ENDCLASS.>
...
oref->do_something(
EXCEPTIONS application_error = 4 ).
IF sy-subrc <> 0.
...
ENDIF.>
ABAP_EXAMPLE_END
ABAP_EXAMPLE_GOOD
The following source code shows the definition of an exception class, its declaration, and the raising in a method as well as its handling using
INHERITING FROM cx_static_check.
ENDCLASS.>
PUBLIC SECTION.
METHODS do_something
RAISING cx_application_error.
ENDCLASS.>
METHOD do_something.
...
RAISE EXCEPTION TYPE cx_application_error.
...
ENDMETHOD.
ENDCLASS.>
...
TRY.
oref->do_something( ).
CATCH cx_application_error.
...
ENDTRY.>
This simple example is perhaps not the most obvious demonstration of the great advantage of class-based exceptions over classic exceptions. However, the advantage is clearly seen in nested procedure calls and the handling of exceptions that were raised in more distant call levels.
ABAP_EXAMPLE_END