Blog Archive

RPGLE monitor on-error example code

How to use CL program MONMSG for RPGLE to check for errors? The monitor group performs conditional error handling based on the status code. It consists of:
  • A MONITOR statement
  • One or more ON-ERROR groups
  • An ENDMON statement.

After the MONITOR statement, control passes to the next statement. The monitor block consists of all the statements from the MONITOR statement to the first ON-ERROR statement. If an error occurs when the monitor block is processed, control is passed to the appropriate ON-ERROR group.

If all the statements in the MONITOR block are processed without errors, control passes to the statement following the ENDMON statement.

The monitor group can be specified anywhere in calculations. It can be nested within IF, DO, SELECT, or other monitor groups. The IF, DO, and SELECT groups can be nested within monitor groups.

If a monitor group is nested within another monitor group, the innermost group is considered first when an error occurs. If that monitor group does not handle the error condition, the next group is considered.

The following RPGLE sample code for monitor on-error submits a job to create a file in the IFS and then tries to use that file. At first it checks whether the file exists or not. After it finds the file, its possible that the program creating the file may still be writing data to it so it may be in use causing the copy object command to fail. If the copy object fails due to object in use error (CPFA09E) then the program waits for more time and tries again.

h option(*nodebugio)                                 
d $cmp            s              2a                  
d $ord            s              6a                  
d $bocd           s              2a                  
d $session        s             24a                  
d $status         s              1a                  
d dtaq_name       s             10a                  
d dtaq_lib        s             10a                   
d dtaq_len        s              5p 0                   
d dtaq_data       s            256                      
d $web_path       s            101a                     
d $pdf_path       s            101a                     
d $path           s             50a   inz('/directory')   
d $pdf_name       s             50a                     
d $found          s              1a                     
d $count          s              3s 0                   
d @CmdStr         s            512a   inz               
d @Apostr         s              1a   inz(X'7D')        
 *  Program Information                                 
d progstatus     sds                                    
d  parms            *parms                              
d  progname         *proc                               
d  errmsgID              40     46                      
d  errmsg                91    169                      
d  jobname              244    253                      
d  userid               254    263                      
d  jobnumber            264    269                      
d ExcCmd          PR                  ExtPgm('QCMDEXC') 
d  Cmd_Str                     512a   Options(*VARSIZE)          
d                                     Const                      
d  Cmd_Len                      15p 5 Const                      
c     *entry        plist                                        
c                   parm                    $cmp                 
c                   parm                    $ord                 
c                   parm                    $bocd                
c                   parm                    $session             
c                   parm                    $status              
c                   eval      $status = *blanks                  
c                   eval      $pdf_name = %trim(userid) + '.PDF'  
c                   eval      dtaq_data = $cmp +           
c                                         $ord +           
c                                         $bocd +          
c                                         $path +          
c                                         $pdf_name        
c*--- Sending request to Data Queue to generate my PDF file                                                           
c                   call      'QSNDDTAQ'                   
c                   parm      '@INVOICE'    dtaq_name      
c                   parm      '*LIBL'       dtaq_lib       
c                   parm      256           dtaq_len       
c                   parm                    dtaq_data      
c                   eval      $path = %trim($path) + '/'   
c                   eval      @CmdStr = 'DLYJOB DLY(5)'    
c                   callp     ExcCmd(%Trim(@CmdStr) :      
c                                   %Len(%Trim(@CmdStr)))  
c                   dou       $count > 20                  
c                   call      'CHKIFS'                     
c                   parm                    $path          
c                   parm                    $pdf_name      
c                   parm                    $found         
c                   if        $found = 'Y'                         
c                   leave                                          
c                   endif                                          
c                   eval      @CmdStr = 'DLYJOB DLY(3)'            
c                   callp     ExcCmd(%Trim(@CmdStr) :              
c                                   %Len(%Trim(@CmdStr)))          
c                   eval      $count = $count + 1                  
c                   enddo                                          
c                   if        $found = 'Y'                         
c                   eval      $web_path = '/MySite/InvoicePdf/' +  
c                                         %trim($session) + '.pdf' 
c                   eval      $pdf_path = %trim($path) +           
c                                         %trim($pdf_name)         
c                   eval      @CmdStr = 'CPY OBJ(' + @Apostr +     
c                                       %trim($pdf_path) +         
c                                       @Apostr + ') TOOBJ(' +     
c                                       @Apostr +                  
c                                       %trim($web_path) +         
c                                       @Apostr + ') REPLACE(*YES)'
    $count = 0;                                                    
    dou $count > 10;                                               
          callp ExcCmd(%Trim(@CmdStr) :                            
          when errmsgID = 'CPFA09E';                               
             callp ExcCmd('DLYJOB DLY(3)':13);                     
             $count = $count + 1;                                  
      $status = 'Y';                                               
c                   endif                 
c                   eval      *inlr = *on 
c                   return