Return to the RPG Tips
Error handling
When handling errors in an RPG program is it better to use the *PSSR with the SDS, or import the _EXCP_MSGID? or is there really a difference? Besides the Number of lines of code! Example 1: Define a Program-Status-Datastructure On Position 40-46 you'll receive the Message-Id and on position 91-170, you'll receive the message text. D PGMSDS SDS D SDSMsgId 40 46 D SDSMsgTxt 90 170 D SDSMsgTxtS 90 140 *------------------------------------------------------------------ /Free P$Cmd = 'CHKOBJ OBJ('+ %Trim(P$LIB) + '/' + %Trim(P$FILE) + ') ' + 'OBJTYPE(*FILE)'; CallP(E) QCmdExc(%Trim($CvtUpper(P$Cmd)): %Len(%Trim(P$Cmd))); If %Error and SDSMsgId = 'CPF9801'; //File not Found; //Create new File ElseIf %Error; Dsply SDSMsgTxtS; EndIf; //Instead of CallP and (E)-Extender, you also can use a monitor group //to trap the Error Monitor; QCmdExc(%Trim($CvtUpper(P$Cmd)): %Len(%Trim(P$Cmd))); On-Error; If DSDMsgId = 'CPF9801'; //Create new File Else; Dsply SDSMsgTxtS; EndIf; EndMon; Example 2: * ... D ErrCPF S 7 import('_EXCP_MSGID') * ... the file keithm doesn't exist. C If System('CLRPFM FILE(keithm)') <> 0 C ErrCPF Dsply C EndIf or Using Example 3: * ... D PgmSts SDS D ErrTyp 40 42 D CPFNum 43 46 * ... the file keithm doesn't exist. C Eval rc = System('CLRPFM FILE(keithm)') * ... C *PSSR BegSR C Eval ErrCPF = ErrTyp + CPFNum C ErrCPF dsply C EndSR
[report a broken link by clicking here]