5.1 Handling the Run-Time Error

You can enhance your application that you built in the previous exercise. The run-time error problem can be fixed.

When you ran your application, a run-time error displayed when you clicked OK in the Customer Inquiry window CUSTINQ the second time. This is because the SHOWWIN operation code is executed again which attempts to Create the Customer Information window again. Since it is already created, the run-time error occurs. The run-time error displays because you have not coded any error handling in the application.

A typical technique in RPG is to use an error indicator on the operation code to handle the error, and then use a conditional statement to handle the error depending on the state of the error indicator.

The logic inside the IF condition uses the SETATR operation code to make the Customer Information window CUSTINFO visible and to give it focus if the SHOWWIN error indicator is on indicating the window is already loaded. Remember that clicking OK on the Customer Inquiry window made this window visible.

Follow these steps:

  1. Edit the press action subroutine for the OK button in the Customer Inquiry window CUSTINQ.
  2. Add an Error Indicator to the SHOWWIN operation code statement. This traps the run-time error.
  3. Add the two lines of code after SHOWWIN as shown in in the following code to display the Customer Information window CUSTINFO. Remember to add 51 to the ShowWin line for the Lo value.
  4. 
    *...1....+....2....+....3....+....4....+....5....+....6....+...7....+.
    CSRN01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq
     *
    C     PSBOK         BEGACT    PRESS         CUSTINQ
     *
    C                   Read      'CustInq'
    C     CustNo        Chain     CustomL3                           50
     *
    C                   If        *IN50 = *OFF
    C                   ShowWin   'CustInfo'                           51
    C                   Eval      %Setatr('CustInfo':'CustInfo':'Visible')=1
    C                   Eval      %Setatr('CustInfo':'CustInfo':'Focus')=1
    C                   Write     'CustInfo'
    C                   EndIf
     *
    C                   ENDACT
     *
    

    Remember to save the change in the Editor before you build your application again.

Running the Application

Now you can run the improved application.

Follow these steps.

  1. After successfully building your application, run it again.
  2. Enter a valid customer number then click OK to display the second window with the customer information.
  3. Now click OK on the Customer Information window. You return to the Customer Inquiry window.
  4. Click OK on the Customer Inquiry window again. The run-time error that you received before does not appear, so your error handling code is working.
  5. Click EXIT to exit the the Customer Inquiry window and close the application.

Click on to indicate that you've done all the steps.