Skip to content

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

Exceptions, RAISE

This example demonstrates the statement RAISE EXCEPTION.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: oref    TYPE REF TO cx_demo_constructor,
          text     TYPE string,
          position TYPE i.
    TRY.
        TRY.
            RAISE EXCEPTION TYPE cx_demo_constructor
              EXPORTING
                my_text = sy-repid.
          CATCH cx_demo_constructor INTO oref.
            text = oref->get_text( ).
            oref->get_source_position(
              IMPORTING  source_line  = position ).
            cl_demo_output=>WRITE_text( |{ position } { text }| ).
            RAISE EXCEPTION oref.
        ENDTRY.
      CATCH cx_demo_constructor INTO oref.
        text = oref->get_text( ).
        oref->get_source_position(
          IMPORTING source_line  = position ).
        cl_demo_output=>WRITE_text( |{ position } { text }| ).
    ENDTRY.
    cl_demo_output=>display( ).

Description

This example shows the two variants of the statement RAISE EXCEPTION. The first statement raises an exception of the class CX_DEMO_CONSTRUCTOR in the inner TRY block, creates the relevant object, and passes the program name to the instance constructor. The inner CATCH block handles the exception, provides the exception text, and raises the exception again without creating a new object. The outer CATCH block handles the exception again. The CX_DEMO_CONSTRUCTOR class is defined in such a way that the passed program name appears in the exception text. The generated instance constructor takes care of this.

The line number in which the exception was raised is shown to indicate that, when the existing exception object was reused, information relevant to this object was modified.