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:

  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:

  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:

  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:

  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

Application Logging in ABAP