IBM AS/400 Frozen Dessert Maker User Manual


 
*=================================================================*
* NOLOOP: Show how to avoid recursion in a *PSSR subroutine. *
*=================================================================*
*-----------------------------------------------------------------*
* Array that will be used to cause an error *
*-----------------------------------------------------------------*
D Arr1 S 10A DIM(5)
*-----------------------------------------------------------------*
* Generate an array out of bounds error to pass control to *PSSR. *
*-----------------------------------------------------------------*
C Z-ADD -1 Neg1 5 0
C MOVE Arr1(Neg1) Arr1(Neg1)
C MOVE *ON *INLR
*=================================================================*
* *PSSR: Error Subroutine for the procedure. We use the *
* variable InPssr to detect recursion in the PSSR. *
* If we detect recursion, then we *CANCL the procedure. *
*=================================================================*
C *PSSR BEGSR
C IF InPssr = 1
C MOVE '*CANCL' ReturnPt 6
C Z-ADD 0 InPssr 1 0
C ELSE
C Z-ADD 1 InPssr
* *
* We now generate another error in the PSSR to see *
* how the subroutine cancels the procedure. *
* *
C MOVE Arr1(Neg1) Arr1(Neg1)
* *
* Note that the next two operations will not be *
* processed if Neg1 is still negative. *
* *
C MOVE '*GETIN' ReturnPt
C Z-ADD 0 InPssr
C ENDIF
C ENDSR ReturnPt
Figure 115. Avoiding a Loop in an Error Subroutine
To create the program and start debugging it, using the source in Figure 115, type:
CRTBNDRPG PGM(MYLIB/NOLOOP) DBGVIEW(*SOURCE)
STRDBG PGM(MYLIB/NOLOOP)
Set a break point on the BEGSR line of the *PSSR subroutine so you can step
through the *PSSR subroutine.
When you call the program, the following occurs:
1. An exception occurs when the program tries to do a MOVE operation on an
array using a negative index. Control is passed to the *PSSR.
2. Since this is the first time through the *PSSR, the variable In_Pssr is not
already set on. To prevent a future loop, the variable In_Pssr is set on.
3. Processing continues within the *PSSR with the MOVE after the ELSE. Again,
an exception occurs and so processing of the *PSSR begins anew.
4. This time through, the variable In_Pssr is already set to 1. Since this indicates
that the subroutine is in a loop, the procedure is canceled by setting the
ReturnPt field to *CANCL.
236 ILE RPG for AS/400 Programmer's Guide