The system field SY-SUBRC
must be tested immediately after any statement setting this variable. Reading this variable informs on
previous operation success or errors. Such errors should be handled properly so that the program continues in a consistent state.
This rule raises an issue when the field SY-SUBRC
is not checked just after performing one of the following operations:
- Calling a function or method which can throw exceptions.
- Calling one of the file access operation
OPEN DATASET
, READ DATASET
or DELETE DATASET
.
SY-SUBRC
check must be done either with the CASE
, IF
or CHECK
statement.
Noncompliant code example
In the following case nothing happens if the exceptions NOT_FOUND
or OTHERS
are raised:
CALL FUNCTION 'STRING_SPLIT'
EXPORTING
DELIMITER = ':'
STRING = FELD
IMPORTING
HEAD = HEAD
TAIL = TAIL
EXCEPTIONS
NOT_FOUND = 1
OTHERS = 2.
Compliant solution
CALL FUNCTION 'STRING_SPLIT'
EXPORTING
DELIMITER = ':'
STRING = FELD
IMPORTING
HEAD = HEAD
TAIL = TAIL
EXCEPTIONS
NOT_FOUND = 1
OTHERS = 2.
CASE SY-SUBRC.
WHEN 1. ...
WHEN 2. ...
WHEN OTHER.
ENDCASE.
Exceptions
No issue will be raised in the following cases:
- One or more
WRITE
operation are performed between the statement setting SY-SUBRC
and its check. An exception will be
however raised if the WRITE
operation is a WRITE ... TO
, as this will set SY-SUBRC
too.
-
SY-SUBRC
's value is assigned to a variable. We then assume that it will be checked later.
OPEN DATASET my_dataset FOR INPUT IN TEXT MODE ENCODING DEFAULT. " Compliant
WRITE 'Test'. " WRITE is accepted before checking SY-SUBRC
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
OPEN DATASET my_dataset FOR INPUT IN TEXT MODE ENCODING DEFAULT. " Compliant
Tmp = SY-SUBRC. " Assigning SY-SUBRC value to a variable. We assume that it will be checked later.
IF Tmp <> 0.
EXIT.
ENDIF.