SAP EXIT PROCEDURE - Guide

Get Example source ABAP code based on a different SAP table
  


GUIDELINE 6.43

Exiting Procedures

ABAP_BACKGROUND
You can exit procedures in the usual way using the END... statement or by using one of the following statements:
RETURN
EXIT
CHECK log_exp
These statements end a procedure properly, that is, the system passes output parameters for which passing by value is specified and returns values to the assigned actual parameters. In addition, you can terminate the processing of a procedure as follows, whereby the actual parameters are not supplied with values:
Calling another unit (program, dynpro) without returning to the procedure
Triggering an exception or sending a dialog message if an error occurs

ABAP_RULE
Only use RETURN to exit procedures
Use the RETURN statement to properly exit a procedure (method, see rule no implementations in function modules and subroutines) early.

ABAP_DETAILS
The RETURN statement serves to exit procedures and always has this result. The behavior of the EXIT and CHECK statements (conditional exit), in contrast, is context- dependent: Within a loop, only the loop is exited; outside a loop, the surrounding procedure is exited. This ambiguity limits the legibility of source code. Therefore, EXIT and CHECK should only be used to exit loops, and RETURN only to exit procedures. Only RETURN enables you to exit a procedure in a loop context.



Latest notes:

As well as the statements RETURN, EXIT, and CHECK listed here, the statements REJECT and STOP can be used to exit special event blocks. Conversely, RETURN, EXIT, and CHECK can also exit processing blocks other than procedures. In both cases, you must consider the particular behavior of the ABAP runtime environment regarding the exited processing block. Because other processing blocks are only supposed to contain one method call according to the rules use ABAP Objects and no implementations in dialog modules and event blocks, these cases should no longer occur in new programs.

Exception
An exception to the rule to only use RETURN to exit procedures are CHECK statements that are located at the beginning of a procedure and that check the prerequisites for the execution of the procedure there. Using the CHECK statement in such a way does not impair the legibility and is thus allowed. However, this exception does not apply to other positions within a procedure and outside loops.

Bad example
The following source code shows how a method is left early with a CHECK statement, whose meaning cannot be identified by simply looking at it. You have to know that CHECK exits the procedure if the following logical expression is wrong, which is why a double negation is necessary here. METHOD some_method.
...
CHECK is_finished = abap_false.
...
ENDMETHOD.

Good example
The following source code corrects and simplifies the above example by implementing a conditional exit with an IF control structure that is easy to read. METHOD some_method.
...
IF is_finished = abap_true.
RETURN.
ENDIF.
...
ENDMETHOD.