Skip to content

Error Handling

Daniel Long edited this page Sep 29, 2026 · 4 revisions

Error Handling

MONITOR / ON-ERROR

MONITOR;
  result = numerator / denominator;
ON-ERROR;
  DSPLY 'Division error!';
  result = 0;
ENDMON;

*PSSR (Program Status Subroutine)

BEGSR *PSSR;
  DSPLY ('Unhandled error: ' + %CHAR(%STATUS));
ENDSR;

Subroutines, *PSSR included, come after all other calculations in their procedure: once the first BEGSR appears, only more subroutines may follow. As on IBM i, a statement after an ENDSR is an error (RNF5005).

ON-EXIT

DCL-PROC ProcessData;
  DCL-PI *N;
  END-PI;
  DCL-S abnormal IND;

  // ... processing ...

  ON-EXIT abnormal;
    IF abnormal;
      DSPLY 'Procedure ended abnormally';
    ENDIF;
    // cleanup code always runs
END-PROC;

DUMP

DUMP writes a formatted dump of the program's variables -- but, as on IBM i, only when the program is compiled with CTL-OPT DEBUG(*YES) (or DEBUG(*DUMP), or H DEBUG in fixed format), or when the operation is DUMP(A). Otherwise it does nothing, so DUMP statements can stay in a program and only fire in a debug build. IBM i writes the dump to a spooled file; OpenRPG prints it.

CTL-OPT DEBUG(*YES);
DUMP;        // dumps: DEBUG(*YES)
DUMP(A);     // dumps whatever DEBUG says

Clone this wiki locally