ABAP Keyword Documentation →  ABAP Programming Guidelines →  Architecture →  Error Handling → 

Classical and Class-Based Exceptions

Background

For reasons of downward compatibility, there are two options to define treatable exceptions yourself in ABAP:

The coexistence of the two exception concepts is regulated as follows:

Rule

Using Class-Based Exceptions

Only raise class-based exceptions in new procedures, provided that you can dispense with classical exceptions from the technical point of view.

Details

Self-defined classical exceptions are little more than return values. If you raise a classical exception in a procedure using the RAISE statement, the sy-subrc system field is set according to the raised exception after the return to the calling program. The calling program itself must always check, by querying sy-subrc, whether an exception occurred and react to it if required, for example, by appropriate handling or explicit forwarding to its own calling program (by raising a separate equivalent exception). This does not improve the clarity of the program.

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 (method) has to consider every possible exception situation itself. This supports the separation of concerns within an application. Because the exception can be represented by an object of an exception class, this exception object can gather additional information on the exception situation and transport it to the handler. In contrast to classical exceptions, this can also include specific exception texts.

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 (RESUMABLE). A handler can decide whether a service is canceled completely or is resumed using the RESUME statement, for example after a corresponding log entry has been written.

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 RAISE EXCEPTION. In this way, you achieve a class-based error handling that is uniform to the outside. The exception situation can then be forwarded to higher call layers without each layer having to react to this situation explicitly.

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).

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 sy-subrc after a call of the method. This procedure infringes the above rule. .

CLASS application DEFINITION.
  PUBLIC SECTION.
    METHODS do_something
    EXCEPTIONS application_error.
ENDCLASS.
CLASS application IMPLEMENTATION.
  METHOD do_something.
    ...
    RAISE application_error.
    ...
  ENDMETHOD.
ENDCLASS.
...
... oref TYPE REF TO application.

...

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 CATCH after the call of the method in a TRY block.

CLASS cx_application_error DEFINITION
  INHERITING FROM cx_static_check.
ENDCLASS.
CLASS application DEFINITION.
  PUBLIC SECTION.
    METHODS do_something
      RAISING cx_application_error.
ENDCLASS.
CLASS application IMPLEMENTATION.
  METHOD do_something.
    ...
    RAISE EXCEPTION TYPE cx_application_error.
    ...
  ENDMETHOD.
ENDCLASS.
...
... oref TYPE REF TO application.

...

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.