SAP RETURN CODE - Guide



Get Example source ABAP code based on a different SAP table
  



Return Code

ABAP_BACKGROUND
The most prominent system field is probably the return code sy-subrc , which signals the successful execution of an ABAP statement or, if you are using classic exceptions, of a procedure. A return code of 0 generally indicates successful execution.

ABAP_RULE
Evaluate the return code sy-subrc
Evaluate the return code sy-subrc after every ABAP statement that sets the value according to the documentation. However, sy-subrc should never be set after statements are executed for which the setting of a return code is not documented.

ABAP_DETAILS
The system field sy-subrc indicates whether a statement was successfully executed. If the execution was not successful, the program should usually react accordingly. If this does not happen, the program's response will probably be unexpected.
This rule is a specialization of the more general rule evaluate system fields in the correct place . It is specified here again separately due to the prominent role of the return code sy-subrc. The system field sy-subrc must always be evaluated immediately and, if necessary, assigned to a help variable. An evaluation cannot occur after statements that set sy-subrc not defined, because otherwise wrong conclusions could easily be drawn.



Latest notes:

Special care must be taken in functional method calls. If executed successfully, each method call sets the return code sy-subrc to 0, which means that functional method calls overwrite the return code when used in statements where the setting of the return code is not documented. The same applies to the instance operator NEW when instances of classes are created.
ABAP_HINT_END

Exception
If a handling action seems unnecessary because, in the developer's opinion, the statement is always executed successfully, the assumption should at least be saved and documented using an assertion.

ABAP_EXAMPLE_BAD
The following source code shows how further processing of data is performed using the work area of a SELECT statement, without a request from sy-subrc. However, the content of wa is usually undefined here, if a request from sy-subrc does not guarantee that database access will be successful. SELECT ...
INTO wa
...
... 'work with wa
ABAP_EXAMPLE_END

ABAP_EXAMPLE_GOOD
The following source code corrects the above example. This means the successful execution of the SELECT statement is checked. SELECT ...
INTO wa
...
IF sy-subrc <> 0.
...
ENDIF
... 'work with wa
ABAP_EXAMPLE_END