SAP ABAP LANGUAGE EXCEPTIONS

Get Example source ABAP code based on a different SAP table
  


Article

Exceptions in ABAP Statements
Error situations,that occur during the execution of an ABAP statement raise exceptions. These exceptions are completely integrated into the exception concept and are triggered by the runtime environment. There are:
handleable exceptions, which are based on predefined exception classes.
non-handleable exception, which produce runtime errors directly.
Each handleable exception is associated with a runtime error. The program terminates with this error if the exception is neither handled nor propagated to a caller. The keyword documentation lists the type of exceptions that can be raised for each statement.
For reasons of backward compatibility, handleable exceptions raised by many ABAP statements can be caught by using both TRY ... ENDTRY and the obsolete statement CATCH SYSTEM-EXCEPTIONS ... ENDCATCH. For this to be possible, the runtime error assigned to the exception class must be catchable. Within processing blocks, the two mechanisms prevent each other from handling exceptions. It is advisable to catch an exception between TRY ... ENDTRY using CATCH or to use the RAISING addition in the definition of the interface to propagate it to the caller. Catching exceptions using CATCH SYSTEM-EXCEPTIONS is not longer recommended.

Example

Unhandled exception
The following program lines produce the runtime error COMPUTE_INT_ZERODIVIDE because division by zero is invalid and this exception situation is not handled:
DATA result TYPE i.
result = 1 / 0.

Handling exceptions using exception classes
The above exception is represented by the exception class CX_SY_ZERODIVIDE, which is a subclass of the exception class CX_SY_ARITHMETIC_ERROR. This means that the exception can be handled as follows (the ERR_TEXT variable is passed the text 'Division by zero.'): DATA myref TYPE REF TO cx_sy_arithmetic_error.
DATA err_text TYPE string.
DATA result TYPE i.
TRY.
result = 1 / 0.
CATCH cx_sy_arithmetic_error INTO myref.
err_text = myref->get_text( ).
ENDTRY.

Handling exceptions as catchable runtime errors
The runtime error COMPUTE_INT_ZERODIVIDE is catchable and assigned to the exception group ARITHMETIC_ERRORS, which means it can also be handled using the obsolete statement CATCH SYSTEM-EXCEPTIONS. DATA result TYPE i.
CATCH SYSTEM-EXCEPTIONS arithmetic_errors = 4.
result = 1 / 0.
ENDCATCH.
IF sy-subrc = 4.
...
ENDIF.