Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Program Flow →  Exception Handling →  Class-Based Exceptions →  Examples of Exceptions 

Exceptions - CATCH

The example demonstrates the catching of class based exceptions.

Other versions: 7.31 | 7.40 | 7.54

Source Code

REPORT demo_catch_exception.

PARAMETERS resumble AS CHECKBOX.
SELECTION-SCREEN ULINE.
PARAMETERS: aft_unw  RADIOBUTTON GROUP rad,
            bef_unw  RADIOBUTTON GROUP rad.
SELECTION-SCREEN ULINE.
PARAMETERS resume AS CHECKBOX.

CLASS lcx_exception DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.

CLASS exc_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: main,
                   meth1 RAISING lcx_exception,
                   meth2 RAISING RESUMABLE(lcx_exception).
ENDCLASS.

FIELD-SYMBOLS <fs> TYPE ANY.

CLASS exc_demo IMPLEMENTATION.
  METHOD main.
    DATA exc TYPE REF TO lcx_exception.
    IF aft_unw = 'X'.
      TRY.
          WRITE / 'Trying method call'.
          IF resumble = ' '.
            exc_demo=>meth1( ).
          ELSEIF resumble = 'X'.
            exc_demo=>meth2( ).
          ENDIF.
        CATCH lcx_exception.
          IF <fs> IS ASSIGNED.
            WRITE / 'Context of method available'.
          ELSE.
            WRITE / 'Context of method not available'.
          ENDIF.
      ENDTRY.
      WRITE / 'Continue after main TRY block'.
    ELSEIF bef_unw = 'X'.
      TRY.
          WRITE / 'Trying method call'.
          IF resumble = ' '.
            exc_demo=>meth1( ).
          ELSEIF resumble = 'X'.
            exc_demo=>meth2( ).
          ENDIF.
        CATCH BEFORE UNWIND lcx_exception INTO exc.
          IF <fs> IS ASSIGNED.
            WRITE / 'Context of method available'.
          ELSE.
            WRITE / 'Context of method not available'.
          ENDIF.
          IF resume = 'X'.
            IF exc->is_resumable = 'X'.
              RESUME.
            ELSE.
              WRITE / 'Resumption not possible'.
            ENDIF.
          ENDIF.
      ENDTRY.
      WRITE / 'Continue after main TRY block'.
    ENDIF.
  ENDMETHOD.
  METHOD meth1.
    DATA loc TYPE i.
    ASSIGN loc TO <fs>.
    TRY.
        WRITE / 'Raising non-resumable exception'.
        RAISE EXCEPTION TYPE lcx_exception.
        WRITE / 'Never executed'.
      CLEANUP.
        WRITE / 'Cleanup in method'.
    ENDTRY.
    WRITE / 'Continue after TRY block in method'.
  ENDMETHOD.
  METHOD meth2.
    DATA loc TYPE i.
    ASSIGN loc TO <fs>.
    TRY.
        WRITE / 'Raising resumable exception'.
        RAISE RESUMABLE EXCEPTION TYPE lcx_exception.
        WRITE / 'Resuming method'.
      CLEANUP.
        WRITE / 'Cleanup in method'.
    ENDTRY.
    WRITE / 'Continue after TRY block in method'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  exc_demo=>main( ).

Description

The meth1 method raises a non-resumable exception, the meth2 method raises a resumable exception that is handled in the TRY-control structure of the method main using CATCH.

  • If handling takes place without BEFORE UNWIND, the CLEANUP block is executed in both cases before handling and the context of the method called is not available during handling.
  • If handling takes place with BEFORE UNWIND, the context of the method called is available in both cases during handling and the CLEANUP block is executed after the handling.
  • When a resumable exception is raised, the RESUME statement can be executed during the handling. This statement makes sure that processing in the method called is continued without its CLEANUP block being executed.