Skip to content

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

Exceptions, TRY

This example demonstrates the TRYcontrol structure.

Other versions: 7.31 | 7.40 | 7.54

Source Code

REPORT demo_try.

PARAMETERS number TYPE i.

CLASS try_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA: result TYPE p LENGTH 8 DECIMALS 2,
                oref   TYPE REF TO cx_root,
                text   TYPE string.
    CLASS-METHODS main.
  PRIVATE SECTION.
    CLASS-METHODS calculation
      IMPORTING  p_number LIKE number
      CHANGING p_result LIKE result
               p_text   LIKE text
      RAISING  cx_sy_arithmetic_error.
ENDCLASS.

CLASS try_demo IMPLEMENTATION.
  METHOD main.
    DATA msg TYPE c LENGTH 40.
    TRY.
        IF ABS( number ) > 100.
          RAISE EXCEPTION TYPE cx_demo_abs_too_large.
        ENDIF.
        calculation( EXPORTING p_number = number
                     CHANGING  p_result = result
                               p_text   = text ).
      CATCH cx_sy_arithmetic_error INTO oref.
        text = oref->get_text( ).
      CATCH cx_root INTO oref.
        text = oref->get_text( ).
    ENDTRY.
    IF NOT text IS INITIAL.
      MESSAGE text TYPE 'I' DISPLAY LIKE 'E'.
    ENDIF.
    msg = |Final result: { result ALIGN = LEFT }|.
    MESSAGE msg TYPE 'I'.
  ENDMETHOD.
  METHOD calculation.
    DATA msg TYPE c LENGTH 40.
    DATA l_oref TYPE REF TO cx_root.
    TRY.
        p_result =  1 / p_number.
        msg = |Result of division: { p_result ALIGN = LEFT }|.
        MESSAGE msg TYPE 'I'.
        p_result = SQRT( p_number ).
        msg = |Result of square root: { p_result ALIGN = LEFT }|.
        MESSAGE msg TYPE 'I'.
      CATCH cx_sy_zerodivide INTO l_oref.
        p_text = l_oref->get_text( ).
      CLEANUP.
        CLEAR p_result.
    ENDTRY.
  ENDMETHOD.
ENDCLASS.

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

Description

  • If the content of number is larger than 100, in the TRY block the TRY control structure of the framework program will trigger an exception of the class CX_DEMO_ABS_TOO_LARGE. This exception is handled by the second CATCH block of the same TRY control structure since the subclass of the most general exception is CX_ROOT.

  • If the content of number is 0, the runtime environment in the TRY block of the TRY control structure of the called method calculation triggers an exception of the predfined class CX_SY_ZERODIVIDE. This is handled in the CATCH block of the same TRY control structure.

  • If the content of number is a negative number, the runtime environment in the TRY block of the TRY control structure of the called method calculation triggers an exception of the predefined class CX_SY_ARG_OUT_OF_DOMAIN. Since in this TRY control structure there is no handler defined for this exception, the exception is propagated from the method - which is possible through the declaration of the superclass CX_SY_ARITHMETIC_ERROR with RAISING in the method interface. Beforehand, the CLEANUP block of the inner TRY control structure is executed.

  • Other possible exceptions are handled in the last CATCH block of the TRY control structure of the framework program. This block catches all possible exceptions through the specification of the most general exception class CX_ROOT. If, for example, CX_SY_ARG_OUT_OF_DOMAIN or one of its superclasses is not declared in the method interface, you would have the exception CX_SY_NO_HANDLER, which would be handled in the last CATCH block.