TRY

Short Reference

Syntax

TRY.
    [try_block]
  [CATCH cx_class1 cx_class2 ... [INTO oref].
    [catch_block]]
    ...
  [CLEANUP [INTO oref].
    [cleanup_block]]
ENDTRY.

Effect

The statement TRY introduces a control structure with several statement blocks. The first statement block try_block is always run, whereas a branching off to exactly one of the remaining statement blocks only occurs if a class-based exception occurs in the try_block.

A TRY control structure defines the following statement blocks:

A TRY control structure precludes the simultaneous use of the CATCH SYSTEM-EXCEPTIONS statement to catch catchable runtime errors in the current processing block.

Notes

Example

Catching class-based exceptions.

PARAMETERS number TYPE i.
DATA: result TYPE p LENGTH 8 DECIMALS 2,
      oref   TYPE REF TO cx_root,
      text   TYPE string.

TRY.
    IF ABS( number ) > 100.
      RAISE EXCEPTION TYPE cx_demo_abs_too_large.
    ENDIF.
    PERFORM calculation USING    number
                      CHANGING result
                               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.
  WRITE / text.
ENDIF.

WRITE: / 'Final result:', result.

FORM calculation USING    p_number LIKE number
                 CHANGING p_result LIKE result
                          p_text   LIKE text
                          RAISING  cx_sy_arithmetic_error.

  DATA l_oref TYPE REF TO cx_root.

  TRY.
      p_result =  1 / p_number.
      WRITE: / 'Result of division:', p_result.
      p_result = SQRT( p_number ).
      WRITE: / 'Result of square root:', p_result.
    CATCH cx_sy_zerodivide INTO l_oref.
      p_text = l_oref->get_text( ).
    CLEANUP.
      CLEAR p_result.
  ENDTRY.

ENDFORM.