The new 'RETRY' statement in TRY...ENDTRY block

By B.Manoj Kumar

A new statement is introduced by SAP in NetWeaver Enhancement Package 2 called ‘RETRY’,which allows us to retry a TRY  block that raised the exception , right from the start.

The RETRY statement can only be executed in the CATCH block.

If this statement is processed in the CATCH block of an exception the whole TRY block is processed again.

To understand the statement clearly let us take a look at an example:

PARAMETERS P1_VALUE TYPE I .
PARAMETERS P2_VALUE TYPE I .
DATA RESULT TYPE DECIMALS 2.

TRY.

    RESULT 
P1_VALUE / P2_VALUE.

    
CATCH CX_SY_ZERODIVIDE.
       P1_VALUE 
0.
       
RETRY.
  
ENDTRY.

  
WRITE 'FINAL RESULT :' RESULT.

The above simple example demonstrates the use of RETRY statement.

Here if the value of P2_VALUE is zero, then CX_SY_ZERODIVIDE exception is raised , and when it is caught by the CATCH block, we are setting P1_VALUE to zero and the RETRY statement is executed which processes the whole TRY block once again.

Here I am trying to divide 4 by 0.  

 

If we debug and see the values of P1_VALUE and P2_VALUE before the RETRY statement , the value are 4 and 0 respectively.

       

And if we observe the values after the RETRY statement, the value of P1_VALUE  is changed to zero and the whole TRY block is executed once again.

 

So the result will be zero.

    

WORD OF CAUTION

But we have to cautious to correct the cause of exception, otherwise the TRY block results in an endless loop.

Example of a TRY block going in an endless loop when we do not correct the cause of exception.

PARAMETERS P1_VALUE TYPE I .
PARAMETERS P2_VALUE TYPE I .
DATA RESULT TYPE DECIMALS 2.


TRY.

    RESULT 
P1_VALUE / P2_VALUE.

    
CATCH CX_SY_ZERODIVIDE.
      P1_VALUE 
1.

RETRY.
  
ENDTRY.

  
WRITE 'FINAL RESULT :' RESULT.

Here the TRY block repeats endlessly, as we didn’t correct the cause of exception.

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