CLEANUP section in TRY…ENDTRY block

By B.Manoj Kumar

A Clean up section is executed whenever an exception occurs within the TRY block and is not handled by a CATCH within the same TRY block, but is handled by a surrounding TRY block.

Whenever an exception occurs in a TRY block it is caught using CATCH, if the CATCH statements did not catch the exceptions then it results in a runtime error.

Example:  (This example is to show the purpose of CLEANUP section, and should not be related to a real time scenario)

PARAMETERS N1 TYPE I.
PARAMETERS N2 TYPE I.
DATA RESULT TYPE I VALUE 10.

TRY.
           
      RESULT 
RESULT + N1 + N2 ).
      RESULT 
RESULT + N1 / N2 ).
      RESULT 
RESULT + 10.

  
CATCH cx_sy_conversion_no_number.
    
WRITE 'conversion error'.
ENDTRY.  

Here if N2 is zero it results in a runtime error.

Here we can do one thing is to forward the error as the handler did not correct the cause of the exception.

Suppose if we have a nested TRY block and exceptions which are not caught by the inner TRY block can be caught in the outer TRY block.

So, let’s do this.

PARAMETERS N1 TYPE I.
PARAMETERS N2 TYPE I.
DATA RESULT TYPE I VALUE 10.

TRY.
    
TRY.

      
      RESULT 
RESULT + N1 + N2 ).
      RESULT 
RESULT + N1 / N2 ).
      RESULT 
RESULT + 10.

CATCH cx_sy_conversion_no_number.
    
WRITE 'conversion error'.

ENDTRY.

CATCH CX_SY_ZERODIVIDE.
    
WRITE 'DIVIDE BY ZERO EXCEPTION'.

ENDTRY.  

Here the outer TRY block caught the exception which is forwarded by the inner TRY block.

But we have to observe one more thing the value of the variable RESULT is in an inconsistent state. The calculation was interrupted in the middle.

      RESULT RESULT + N1 + N2 ).
      RESULT 
RESULT + N1 / N2 ).
      RESULT 
RESULT + 10.

The exception occurred at second statement and the third statement never got executed and the value of RESULT is in an inconsistent state.  

Let us see definition of CLEANUP section.  

A Clean up block is executed whenever an exception occurs within the  TRY block and is not handled by a CATCH within the same TRY block, but is handled by an surrounding TRY block.

So let’s add a cleanup section

This is the inner TRY block.

PARAMETERS N1 TYPE I.
PARAMETERS N2 TYPE I.
DATA RESULT TYPE I VALUE 10.

TRY.
    
TRY.

      RESULT 
RESULT + N1 + N2 ).
      RESULT 
RESULT + N1 / N2 ).
      RESULT 
RESULT + 10.

    
CATCH cx_sy_conversion_no_number.
    
WRITE 'conversion error'.  

   CLEANUP.

        
WRITE 'THIS IS CLEAN UP SECTION'.
        RESULT  
10.
        
WRITE'VALUE OF RESULT :' RESULT.

    
ENDTRY.

CATCH CX_SY_ZERODIVIDE.
    
WRITE 'DIVIDE BY ZERO EXCEPTION'.

ENDTRY.

As the exception is not caught in the inner TRY block, the CLEANUP section gets executed and resets the value of the RESULT variable.

Now, execute the program.

I am supplying the values of N1 and N2 as 2 and 0 respectively.

OUTPUT :


Here we can see that CLEANUP section got executed and the value of variable RESULT has been reset and the exception has been caught.  

Points to be remembered when using a CLEANUP section:  

1 . A TRY-ENDTRY block must contain only one CLEANUP section otherwise it gives a syntax error.

2 . The CATCH statements must be written before the CLEANUP section.

 3 . Since the purpose of the CLEANUP block is to restore the consistency, after the control passes into a CLEANUP section , it should not be exited illegally.

So the use of statements like RETURN , STOP ..etc are not allowed in a CLEANUP section as these statements would change the control flow.

 

4 . All the exceptions that occur within a CLEANUP section must also be handled in the CLEANUP block only.

What happens if we write the following code in CLEANUP section.

  CLEANUP.

        
WRITE 'THIS IS CLEAN UP SECTION'.
        RESULT  
10 0.
        
WRITE'VALUE OF RESULT :' RESULT.  

It results in an run time error as the exception will not be caught by the outer TRY block, so the exception must be handled inside the CLEANUP section.

So this is very important that no exception is raised in a CLEANUP statement.

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image