SAP CLASS EXCEPTION - Guide
Get Example source ABAP code based on a different SAP table
GUIDELINE 5.9
Classical and Class-Based Exceptions
ABAP_BACKGROUND
For reasons of downward compatibility, there are two options to define treatable exceptions yourself in ABAP:
These exceptions can only be declared in the interfaces of methods or function modules using
These exceptions are defined by exception classes, from which the system may generate an exception object when an exception is raised (if a handler uses the
The coexistence of the two exception concepts is regulated as follows:
ABAP_RULE
Only raise class-based exceptions in new procedures if you can dispense with classical exceptions from the technical point of view.
ABAP_DETAILS
Self-defined classical exceptions are little more than return values. If a classical exception is raised in a procedure using the
The occurrence 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 cancelling an entire service. For these cases, you can raise and propagate class-based exceptions as resumable (
Class-based exceptions completely replace the classical exceptions for new code (of course, there are exceptions to this rule) and add resumability. Although classical exceptions on the raiser side are completely obsolete from a technical point of view, you must still consider the following for older code: Even if you have the raiser side under control, you cannot simply change older procedures over to class-based exceptions, because then you would have to adapt all usage locations.
When you call existing procedures that use classical exceptions, you must continue to handle them in the new code. In this case, we recommend mapping the classical exceptions to equivalent class-based exceptions by using
Exception
Since class-based exceptions are currently not supported in remote-enabled function modules (
Bad example
The following source code shows the declaration and the raising of a classical 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.
Good example
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 classical exceptions. However, the advantage is clearly seen in nested procedure calls and the handling of exceptions that were raised in more distant call levels.