Using an Error Subroutine
When you write a error subroutine you are doing two things:
- Enabling the RPG subroutine error handler
The subroutine error handler will handle the exception and pass control to your subroutine.
- Optionally specifying a recovery action.
You can use the error subroutine to take specific actions based on the error which occurred or you can have a generic action (for example, issuing an inquiry message for all errors).
The following considerations apply to error subroutines:
- You can explicitly call an error subroutine by specifying the name of the subroutine in Factor 2 of the EXSR operation.
- You can control the point where processing resumes in a cycle-main procedure by specifying a value in Factor 2 of the ENDSR operation of the subroutine. In a subprocedure, factor 2 of the ENDSR must be blank. Use either a GOTO or a RETURN operation prior to the ENDSR operation to prevent the subprocedure from ending abnormally.
- If an error subroutine is called, the RPG error subroutine handler has
already handled the exception. Thus, the call to the error subroutine reflects
a return to program processing. If an exception occurs while the subroutine
is running, the subroutine is called again. The procedure will loop unless
you code the subroutine to avoid this problem.
To see how to code an error subroutine to avoid such a loop, see Avoiding a Loop in an Error Subroutine.