Supported RPG Features

MONITOR blocks provide structured exception handling, similar to try-catch in Java. When an exception occurs in the protected block, control passes to the first matching ON-EXCP handler (by message ID), then to the first matching ON-ERROR handler (by status code).

DCL-S result PACKED(5:0);
DCL-S divisor PACKED(5:0) INZ(0);

MONITOR;
  result = 100 / divisor;   // Division by zero
ON-ERROR 102;               // Catch divide-by-zero (status 102)
  result = 0;
ON-ERROR;                   // Catch-all for any other error
  result = -1;
ENDMON;

Key behaviors:

Status code groupings:

Syntax Matches
ON-ERROR 102; Specific status code (divide by zero)
ON-ERROR 102:121:122; Any of the listed codes
ON-ERROR *PROGRAM; All program status codes (00100 - 00999)
ON-ERROR *FILE; All file status codes (01000 - 09999)
ON-ERROR *ALL; All errors (equivalent to bare ON-ERROR;)
ON-ERROR 102 : *FILE; Specific codes mixed with groupings

Common program status codes:

Code Description
00100 String operation error
00102 Divide by zero
00121 Array index not valid
00122 OCCUR value out of range
00222 Pointer or parameter error

Fixed-format support:

MONITOR, ON-ERROR, and ENDMON are supported in both free-format and fixed-format C-specs. In fixed-format, ON-ERROR status codes are specified in Factor 2:

     C                   MONITOR
     C                   ...statements...
     C                   ON-ERROR  00122
     C                   ...handler...
     C                   ENDMON

ON-EXCP - message-ID-based exception handling (free-format only):

ON-EXCP catches exceptions by message ID (e.g. MCH1211, CPF9898) rather than by numeric status code. ON-EXCP clauses must precede any ON-ERROR clauses in the MONITOR group.

DCL-S x PACKED(5:0) INZ(10);
DCL-S y PACKED(5:0) INZ(0);
DCL-S result CHAR(30);

MONITOR;
  x = x / y;                    // Divide by zero
ON-EXCP 'MCH1211';              // Catch by message ID
  result = 'Division by zero';
ON-EXCP 'CPF9898';              // Catch generic escape
  result = 'Generic escape';
ON-ERROR;                        // Catch-all by status code
  result = 'Other error';
ENDMON;