Catching Runtime Errors 

Some runtime errors are caused by errors that make it impossible to permit a program to continue. However, some are not so serious, and these can be handled within the program. Runtime errors are therefore categorized as either catchable or non-catchable. The keyword documentation for each ABAP statement indicates the runtime errors that can occur in connection with the statement, and whether or not they are catchable. Catchable runtime errors are divided into ERROR classes that allow you to handle similar errors together.

You can handle catchable runtime errors in an ABAP program using the following control statements:

CATCH SYSTEM-EXCEPTIONS <exc1> = <rc 1> ... <exc n> = <rc n>.
  ...
ENDCATCH.

The expressions <exci> indicate either a single catchable runtime error or the name of an ERROR class. The expressions <rc i> are numeric literals. If one of the specified runtime errors occurs between CATCH and ENDCATCH, the program does not terminate. Instead, the program jumps straight to the ENDCATCH statement. After ENDCATCH, the numeric literal <rc i> that you assigned to the runtime error is contained in the return code field SY-SUBRC. The contents of any fields that occur in the statement in which the error occurred cannot be guaranteed.

CATCH control structures are like IF structures, that is, you can nest them to any depth, but they must begin and end within the same processing block. Furthermore, CATCH control structures only catch runtime errors at the current call level, and not in any procedures that you call from within the CATCH … ENDCATCH block.

The keyword documentation for the CATCH statement contains a list of all catchable runtime errors and the way in which they are organized into ERROR classes.

 

REPORT demo_catch_endcatch.

DATA: result TYPE p DECIMALS 3,
      number TYPE i VALUE 11.

CATCH SYSTEM-EXCEPTIONS arithmetic_errors = 5.
  DO.
    number = number - 1.
    result = 1 / number.
    WRITE: / number, result.
  ENDDO.
ENDCATCH.

SKIP.

IF sy-subrc = 5.
  WRITE / 'Division by zero!'.
ENDIF.

This program calculates the quotient of 1 and NUMBER ten times until the catchable runtime error BCD_ZERODIVIDE occurs. This belongs to the ERROR class ARITHMETIC_ERRORS, and is caught in the example using this class.