Your Ad Here
Your Ad Here

Thursday, January 8, 2009

Error Handling - Division by Zero in RPG

Here is an example to show how to handle division by zero error in RPG program

dMyPsds sds - Define a variable of type *STATUS to capture return status of the progam
dStatus *status
derror_lineno 21 28 - To capture line on which error has occured

dnstatus s 4s 0 inz(0)
dArgum1 s 4s 0 inz(12)
dArgum2 s 4s 0 inz(0)
dResult s 4s 0 inz(0)

dRestrPoint s 6a
dATemp s 4s 0 inz(0)

c '-+-+-+-+' Dsply
c 'Begin...' Dsply
c '-+-+-+-+' Dsply
c Opr1 Tag
c Eval Result = Argum1 / Argum2 - this will result in error and Arg2 is Zero
c Result Dsply
c 'Hurry ' Dsply
c Eval *inlr = *on
c Return
c*
c*defining Pssr subroutine to handle exception condition
c *pssr BEGSR
c Move status nstatus
c Move error_lineno ATemp

c* is the error divide by zero ? ie 102 ?

c If nstatus = 102
c Eval Argum2=2 The value of Argum2 is changed to 2
c Else
c* error is not a divide by zero End program.
c Eval RestrPoint = '*CANCL'
c Endif

c If RestrPoint <> '*CANCL'
c goto Opr1
c Endif
c Endsr RestrPoint

OUTPUT :
DSPLY -+-+-+-+
DSPLY Start..
DSPLY -+-+-+-+
DSPLY 6
Digg this

Monday, July 28, 2008

File Exception Handling.

As mentioned in my previous post this post is regarding Exception handling in Files in RPG. There are quite a number of ways for performing exception handling in RPG

Using ‘E’ Extender. When ‘E’ extender is specified for performing file operation like CHAIN or READ it set %STATUS and %ERROR to Zero. If there is an error then %ERROR is set to 1 and the status of the file is set in %STATUS.

Another option for exception handling is Setting Indicator in Position 73 74. Depending upon the operation the indicator will be set to ON or OFF if the operation is successful.

Using MONITER OpCode in RPG. When a Statement is using MONITER OpCode and an errors occurs the control is given to ON-ERROR group of statement which will contain exception handling

By the use of INFSR and INFDS. Using INFSR we can specify the subroutine which will receive the control if an exception happens on the file operation and the same is not being handled using ‘E’ extender or Indication in 73 74 position and MONITER OPCODE.
Digg this

Thursday, July 24, 2008

Exception handling in RPG

If you have ever worked on AS400 you would have come across error messages like CPFxxxx while running you pmg/application. I know and you know how frustrating it is. Well let see what we can do mange then so that the program performs a meaning full function rather than then showing the stupid error.


When we execute our program. The program makes request to the Operating system. If the request fails the operating system responds by sending as escape message to the program message queue. When the escape message arrives it is checked whether the program is going to hand the error, if not the system error handling kicks in.

As we all know the most common way for exception handling in RPG is using *PSSR subroutine. I will show you by an example how it works

D PSSROnce S N
D BombProgram PR ExtPgm('BOMBPGM')
BegSR *PSSR;
Dump(A);
If PSSROnce;
*INH1 = *On;
Return;
Else;
PSSROnce = *On;
BombProgram();
EndIf;
EndSR '*CANCL';


The (A) extender means that a DEBUG keyword is not required on the H Spec.

The above code is an example of how *PSSR subroutine will contain. In case the program is not having *PSSR subroutine the program fails with an error message.

The program shown above will perform a DUMP and call program BombProgram. The indicator PSSROnce in the program is added to avoid infinite loop. In case some thing bombs in *PSSR it will again call *PSSR which will fail again and result in infinite calls to *PSSR routine.


*CANCL is to stop the execution of the program when error is occured the other valid values are

*DETL. Continue at the beginning of detail output lines
*GETIN. Input Record Routine
*TOTC. Total Time Calculations
*TOTL. Continue at the beginning of total output lines
*OFL. Continue at the beginning of overflow lines
*DETC. Continue at the beginning of detail calculations.
*CANCL. Cancel the Execution of the program

Now the logic for BombProgram().

DclF FrndlyDsp
Dcl &Counter *Dec (5 0)
Dcl &Type *Char (1)

DspJobLog *Print
SndMsg ('Please help Me') ToMsgQ(QSysOpr)
RtvJobA Type (&Type)
If (&Type = '0') Goto End
Loop: SndRcvF RcdFmt(FRNDLERROR)
ChgVar &Counter (&Counter + 1)
If (&Count < 100) Goto Loop
End: EndJob *Immed

When this program is invocked it will print Job Log, Send msg to the QsysOpr for Help and display a user-friendly screen to the user. It is up to you to decided what needs to be displayed in the screen. The user has to press Enter 100 times to get out of the screen.

In the next post will try to cover more details about exception handeling in respect to Files
Digg this

Program Defined Data Structure (PSDS)

A program status data structure (PSDS) can be defined to make program exception/error information available to an RPG IV program. The PSDS must be defined in the main source section; therefore, there is only one PSDS per module.

A data structure is defined as a PSDS by an S in position 23 of the data structure statement. A PSDS contains predefined subfields that provide you with information about the program exception/error that occurred. The location of the subfields in the PSDS is defined by special keywords or by predefined From and To positions. In order to access the subfields, you assign a name to each subfield. The keywords must be specified, left-adjusted in positions 26 through 39.

Information from the PSDS is also provided in a formatted dump. However, a formatted dump might not contain information for fields in the PSDS if the PSDS is not coded, or the length of the PSDS does not include those fields. For example, if the PSDS is only 275 bytes long, the time and date or program running will appear as N/A. in the dump, since this information starts at byte 276.
D SDS
D PROC_NAME *PROC Procedure name
D PGM_STATUS *STATUS Status code
D PRV_STATUS 16 20S 0 Previous status
D LINE_NUM 21 28 Src list line nu
D ROUTINE *ROUTINE Routine name
D PARMS *PARMS Num passed parms
D EXCP_TYPE 40 42 Exception type
D EXCP_NUM 43 46 Exception number
D PGM_LIB 81 90 Program library
D EXCP_DATA 91 170 Exception data
D EXCP_ID 171 174 Exception Id
D DATE 191 198 Date (DATE fmt)
D YEAR 199 200S 0 Year (YEAR fmt)
D LAST_FILE 201 208 Last file used
D FILE_INFO 209 243 File error info
D JOB_NAME 244 253 Job name
D USER 254 263 User name
D JOB_NUM 264 269S 0 Job number
D JOB_DATE 270 275S 0 Date (UDATE fmt)
D RUN_DATE 276 281S 0 Run date (UDATE)
D RUN_TIME 282 287S 0 Run time (UDATE)
D CRT_DATE 288 293 Create date
D CRT_TIME 294 299 Create time
D CPL_LEVEL 300 303 Compiler level
D SRC_FILE 304 313 Source file
D SRC_LIB 314 323 Source file lib
D SRC_MBR 324 333 Source file mbr
D PROC_PGM 334 343 Pgm Proc is in
D PROC_MOD 344 353 Mod Proc is in
D CURR_USER 358 367 Mod Proc is in


Soon to be followed with working example in RPGLE
Digg this

Tuesday, July 22, 2008

And Finaly

Finally Prime minsiter Manmohan singh government survives the trust vote, which happened yesterday and was telecasted on the entire leading news channel. In a House with strength of 541 MP they needed 271 votes for majority. Initially with Speaker Somnath Chaterjee asked for electronic pools in all there were 487 votes 253 in favor and 235 against 2 member chose not to vote.

Those MP who were not able to vote electronically were give paper chit to vote. After all the votes were counted UPA government win the trust vote by winning 275 votes in favor of the government.
I guess This trust vote will help the UPA government to smooth the road for the N-Deal. Yesterday was a big day for both the ruling government and the opposition. To be honest when LEFT parties withdrew the support from the government I wasn’t sure whether the government would survive or not as LEFT were having around 60 odd MP supporting the government.
Digg this

Friday, July 18, 2008

Yesterday i was going through the google search looking for some new site and look what i found. http://www.free-tv-video-online.info/ you wont believe what you can get on this site. The moment any hollywood picture gets released it is on this site to be watched online - yes - no downloads required. Depending upon the bandwidth the video will be played. One hell of cool site it is already having HellBoy 2 and with in couple of days it will have Dark Night for sure. So guys keep watching the site.
Digg this

Wednesday, July 16, 2008

Well lets begain with india getting ready for N-Deal or the Polls not yet clear. The Governement has to prove there majority on 22 July 2008 in the Parilament house. I am not sure but as a whole the N-Deal look OK. I mean if we are actually able to use it for generation of electricity and other common services.
Digg this