ABAP various ways to Exit processing Blocks

No matter which programming language you are working with, at the end of the day your job revolves around manipulating its processing block to achieve the desired results. One of them is exiting it.
In this post, I wanted to talk about various ways you could exit an ABAP processing block. 
But first,

What is a Processing Block?

    Modularization unit of an ABAP program that cannot be split or nested. Processing blocks are
    - Procedures like subroutines or methods,
    - Dialog modules declared for screens, and 
    - Event blocks like reporting events such as load-of-program, start-of-selection,  etc.

    To summarise, 

        Each non-declared statement in an ABAP program is part of a processing block.

Now, coming back to the point of the post.
Below are the different ABAP statements that you can use to exit the processing block.
-> Always Use RETURN
-> CHECK can be used as a guard clause at the start of the block
-> Use EXIT only inside LOOP blocks
-> Never use STOP.

RETURN:

  Syntax:  RETURN.

  Example:

METHOD show_list.
"IMPORTING it_data TYPE ANY TABLE.
DATA:
lo_alv_list TYPE REF TO cl_gui_alv_grid.
" Leave if input has no data.
IF it_data IS INITIAL.
RETURN.
ENDIF.
cl_salv_table=>factory( IMPORTING r_salv_table = lo_salv " Basis Class Simple ALV Tables
CHANGING t_table = it_data
).
lo_salv->display( ).
ENDMETHOD.

  Features:

  - This statement ends the current processing block immediately.
  - Can be used at any point in the processing block. 
  - The statement ends the block irrespective of the statement block or control structure it is used inside.
     In Other words, 
        Even if you use the statement RETURN inside loops, it will end the whole processing block 
        that contains the loop, not just the loop block. 

  Things to Note: 

   - You cannot exit the load-of-program event block using RETURN.
   - If you use RETURN to exit start-of-selection, No further reporting events are triggered. 
     Instead, the list processor is called directly.

  My Opinion:

   - This is by far the best way to exit procedures. 

EXIT: 

  Syntax:   EXIT.

  Example:

METHOD get_Data.
DATA:
lt_mara TYPE STANDARD TABLE OF mara,
lt_marc TYPE STANDARD TABLE OF marc.
SELECT *
FROM MARA
INTO TABLE LT_MARA
WHERE MATNR = iv_matnr.
" EXIT on unsuccesful Query.
IF SYST-SUBRC <> 0.
EXIT.
ENDIF.
SELECT *
from MARC
INTO TABLE LT_MARC
FOR ALL ENTRIES IN LT_MARA
WHERE MATNR = LT_MARA-MATNR.
" EXIT on unsuccesful Query.
IF SYST-SUBRC <> 0.
EXIT.
ENDIF.
ENDMETHOD.

  Features: 

   - If used outside a loop block, the statement immediately terminates the current processing block.
   - If used inside a loop block, the statement terminates it. 

  Things to Note:

   - You cannot exit the load-of-program event block using EXIT
   - If you use the EXIT to exit the start-of-selection event, no further reporting events
     are triggered. Instead, the list processor is called directly.       

  My Opinion: 

  - The EXIT statement should only be used to terminate loop blocks prematurely.

CHECK: 

  Syntax:   CHECK log_exp.

 Example:

METHOD get_Data.
DATA:
lt_mara TYPE STANDARD TABLE OF mara,
lt_marc TYPE STANDARD TABLE OF marc.
CHECK iv_matnr is not intial." Check if input has value.
SELECT *
FROM MARA
INTO TABLE lt_mara
WHERE MATNR = iv_matnr.
" Leave on unsuccesful Query.
CHECK syst-subrc = 0.
SELECT *
from MARC
INTO TABLE lt_marc
FOR ALL ENTRIES IN lt_mara
WHERE MATNR = lt_mara-MATNR.
" Leave on unsuccesful Query.
CHECK syst-subrc = 0.
ENDMETHOD.

  Features:

  - If used outside a loop and the log_exp is false, the statement immediately terminates the
    current processing block.
  - If used inside a loop and the log_exp is false, the statement skips the current loop pass and
    moves on to the next loop pass.

  Things to Note: 

   - You cannot exit the load-of-program using CHECK.

  My Opinion:

  - The CHECK statement should be used either at the start of the processing block as a 
    guard clause or inside loop block to skip loop passes based on a certain condition.

STOP:

  Syntax:   STOP.

  Example:

REPORT sy-repid.
PARAMETERS:
p_matnr TYPE matnr.
DATA:
lt_mara TYPE STANDARD TABLE OF mara.
START-OF-SELECTION.
SELECT *
FROM mara
INTO TABLE lt_mara
WHERE matnr = p_matnr.
IF syst-subrc <> 0.
STOP. " Stop processing
ENDIF.
cl_demo_output=>display( lt_mara ) .

  Features: 

  - The STOP statement is only to be used in executable programs and in the following event blocks:
       at selection-screen (without additions),
       start-of-selection
       These event blocks are exited using STOP and the runtime environment triggers the event
        end-of-selection.
  - Using STOP at other places will lead to non-catchable exceptions and runtime errors:
       stop_no_report - Executed outside the process for an executable program.
       stop_within_called_dynpro - Executed during the process for a screen and 
       therefore outside the permitted events.

  My Opinion:

    Do not use the STOP statement unless absolutely necessary and there are no other options to 
    achieve the desired results.

In Conclusion,

    In ABAP, There are different options available for program-driven exiting of a processing block 
    with each having its own features.
    However from a robustness point of view RETURN statement is the best option, the second 
    choice being CHECK as a guard clause at the start of the block.
    I hope you liked the post and use this information while in your day-to-day work.

Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET

Use CL_BCS library to send email with ABAP