Saturday, December 7, 2013

Day-16.

 Exception Handling :
 Overview:
This module enables the student to understand the concept of Exception handling.
 Outline:
Topics covered,
·  Input Specification (IPDS, IPJX, IPN)
·  Data  structure
·  File information data structure
·  Program status data structure
Objective:
By the end of this module, the student should be able to:
  • Data structure
  • INFDS, INFSR and  *PSSR data structure
Data Structure:
 The RPG/400 program allows you to define an area in storage and the layout of the fields, called subfields, within the area. This area in storage is called a data structure.

You can use a data structure to:

  •  Define the same internal area multiple times using different data formats
  •  Operate on a field and change its contents
  •  Divide a field into subfields without using the MOVE or MOVEL operation codes
  •  Define a data structure and its subfields in the same way a record is defined
  •  Define multiple occurrences of a set of data
  •  Group non-contiguous data into contiguous internal storage locations.
 In addition, there are three special data structures, each with a specific purpose:
  •  A data area data structure (identified by a U in position 18 of the data structure statement)
  •  A file information data structure (referred to by the keyword INFDS on a file description specifications continuation line)
  •  A program-status data structure (identified by an S in position 18 of the data structure statement).
Data structures can be program-described or externally described.

Data Structure Statement Specifications
Data structure statements are defined on the input specifications and must follow all input specifications for records. The specifications for data structure statements are:

Position
                                                                       Entry

 6

I


7-12

Name of the data structure being defined. This entry is optional for a program-described data structure, and is required for an externally described data structure, a file information data structure (INFDS), and a data area data structure.


13-16

Blank


 17

Blank: Program-described data structure.

E: Externally described data structure. The data structure subfields definitions are retrieved from an externally described record format.


 18

Blank: Other than a program status, data area or initialized data structure.

I     :      Globally initialized data structure.
S   :       Program-status data structure.
U   :       Data area data structure.


19-20


DS

21-30

Blank: The data structure is program described.

Entry: This is the name of the file whose first record format contains the field descriptions used as the subfields descriptions for this data structure.


31-43


Blank

44-47

Blank: A single occurrence data structure.
nnnn: A number (right-adjusted) indicating the number of occur-rences
of the data structure.
Note:  This entry must be blank for a data area data structure, a file
information data structure, and a program-status data structure.


48-51


Length of data structure (optional). This entry must be right-adjusted.

52-74


Blank

Rules for Specifying Data Structure Statements

Remember the following when you specify data structure statements:

  • The data structure name must be a symbolic name with a maximum of six characters. The name can appear on only one data structure specification, cannot be a look ahead field, and can be specified anywhere a character field is allowed.
  • All entries for one data structure and its subfields must appear together; they cannot be mixed with entries for other data structures.
  • The data structure length is determined by the first specification in the program that defines a length in one of the preceding ways. Subsequent conflicting lengths are incorrect. The length of a data structure is one of the following:
    • The length specified in the input-field specifications if the data structure name is an input field
    • The length specified in positions 48 through 51 of the data structure statement
    • The highest To position of a subfield within a data structure if the data structure name is not an input field.
  • A compile-time or prerun-time array cannot be used in a data area data structure or in a multiple-occurrence data structure.
  • Data structures are character data and can be from 1 to 9999 characters in length.
  • A data structure and a subfield of a data structure cannot have the same name.

Special Data Structures

Special data structures include:
  • Data area data structures
  • File information data structures (INFDS)
  • Program-status data structures.

Data Area Data Structure

A data area data structure, identified by a U in position 18 of the data structure statement, indicates to the RPG/400 program that it should read in and lock the data area of the same name at program initialization and should write out and unlock the same data area at the end of the program. Data area data structures, as in all other data structures, have the type character. A data area read into a data area data structure must also be character. The data area and data area data structure must have the same name unless you rename the data area within the RPG/400 program by using the *NAMVAR DEFN statement.

You can specify the data area operations (IN, OUT, and UNLCK) and have the type for a data area that is implicitly read in and written out. Before you use a data area data structure with these operations, you must specify that data area in the result field of the *NAMVAR DEFN statement. A data area data structure cannot be specified in the result field of a PARM operation. If you specify blanks for the data area data structure (positions 7 through 12 of the input specifications line that contains a U in position 18), the RPG/400 program uses a local data area. To provide a name for a local data area, use the *NAMVAR DEFN operation, with *LDA in factor 2 and the name in the result field. If data structure name entry is blank then the local data area is used.

This data area either you can create using CRTDTAARA command or if not created by you then system will create it in QTEMP library at the start of job and will delete it at the end of the job.

Following example shows how we divide fields using subfields using Data Area Data Structure:
Step 1:
Create data area using CRTDTAARA command on command line and press F4:


                          Create Data Area (CRTDTAARA)                         
                                                                               
 Type choices, press Enter.                                                    
                                                                                
 Data area  . . . . . . . . . . .     *DTA1                Name                         
   Library  . . . . . . . . . . .     *CURLIB     Name, *CURLIB                
 Type . . . . . . . . . . . . . .                 *DEC, *CHAR, *LGL, *DDM       
                                                                                                                                                            
                                                                               
                                                                                
                                                                         Bottom
 F3=Exit   F4=Prompt   F5=Refresh   F10=Additional parameters   F12=Cancel     
 F13=How to use this display        F24=More keys                               
                                                                               

Step 2:
Write data area as ‘DTA1’ which is data area name, library name and provide data area type *CHAR.

Data area is having 3 types:
  • Decimal (*DEC)
  • Character (*CHAR)
  • Logical (*LGL)
Step3: after inserting above information press F10 to fill additional attributes like data areas length as shown below:

                                                  Create Data Area (CRTDTAARA)                         
                                                                                
 Type choices, press Enter.                                                    
                                                                               
 Data area  . . . . . . . . . . . > DTA1          Name                         
   Library  . . . . . . . . . . . >   PB01U010    Name, *CURLIB                
 Type . . . . . . . . . . . . . . > *CHAR         *DEC, *CHAR, *LGL, *DDM      
 Length:                                                                        
   Length . . . . . . . . . . . .   20            1-2000                       
   Decimal positions  . . . . . .                 0-9                          
 Initial value  . . . . . . . . .                                               
 Text 'description' . . . . . . .   *BLANK                                     
                                                                               
                                                                                
                            Additional Parameters                              
                                                                               
 Authority  . . . . . . . . . . .   *LIBCRTAUT    Name, *LIBCRTAUT, *CHANGE... 
                                                                                
                                                                               
                                                                               
                                                                         Bottom
 F3=Exit   F4=Prompt   F5=Refresh   F12=Cancel   F13=How to use this display   
 F24=More keys                                                                 
                                                                                
 Press enter to create dta1 in library pb01u01o.

Step 4:
Write simple RPG program to see how Data Area DS is define or use in RPG.

To define Data Area DS use the prompt as given below:
Columns . . . :    1  71            Edit                        PB01U01S/MYRPG
 SEU==>                                                                   DADS1
 FMT DS .....IDsname....NODsExt-file++.............OccrLen+....................
        *************** Beginning of data *************************************
0000.01       * USE IPDS PROMPT HAVING ‘U’ AT ‘OPTION’ TO DEFINE DATA AREA DS.
0001.00      IDTA1       UDS                                                   

  

 Prompt type . . .   DS      Sequence number . . .  0001.00                    
                                                                               
 Data Struct                               External                            
    Name       Number    Option    DS     File Name     Occurs    Length       
   DTA1                    U       DS                                           
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh          F11=Previous record                  
 F12=Cancel            F23=Select prompt   F24=More keys                       
                                                                               
See the following ‘IPJ’ prompt where to define the attributes of subfield.

IPJ :

Positions
Name
Entry
7-42

Blank
43
Data format
Blank
P
B
L
R
44-47
From
1-9999
48-51
To
1-9999
52
Decimal positions
Blank
0-9
53-58
Field name
Symbolic name
59-60
Control Level
Blank
L1-L9
61-62
Match fields
Blank
M1-M9
63-64
Field record relation
Blank
01-99
L1-L9
MR
U1-U8
H1-H9
RT
65-70
Field indicators
Blank
01-99
H1-H9
U1-U8
RT
71-74

Blank
75-80
Comments
Optional


Columns . . . :    1  71            Edit                        PB01U01S/MYRPG
 SEU==>                                                                   DADS1
 FMT DS .....IDsname....NODsExt-file++.............OccrLen+....................
        *************** Beginning of data *************************************
0000.01       * USE IPDS PROMPT HAVING ‘U’ AT ‘OPTION’ TO DEFINE DATA AREA DS.
0001.00      IDTA1       UDS                                                   
0000.10       * USE IPJ PROMPT TO DEFINE SUBFIELDS OF DATA AREA DS.
0002.00      I                                        1   62AMT1               



 Prompt type . . .    J      Sequence number . . .  0002.00                    
                                                                               
                                 Decimal        Field    Control     Matching   
 P/B/L/R      From      To       Positions      Name      Level       Fields    
                 1      6         2              AMT1                            
 Field Record        Positive     Negative     Zero /                          
   Relation           Field        Field       Blank                           
                                                                               
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh          F11=Previous record                 
 F12=Cancel            F23=Select prompt   F24=More keys                        

    

Define other fields ‘AMT2’ and ‘SUM’ using same prompt ‘IPJ’ as shown below. Use *NAMVAR define data area ‘DTA1’ and initialize values 100 and 200.20 to fields AMT1 and AMT2 respectively. Add this amount to variable or subfield SUM.

                                                                                
Columns . . . :    1  71            Edit                        PB01U01S/MYRPG
 SEU==>                                                                   DADS1
 FMT DS .....IDsname....NODsExt-file++.............OccrLen+....................
        *************** Beginning of data *************************************
0000.01       * USE IPDS PROMPT HAVING ‘U’ AT ‘OPTION’ TO DEFINE DATA AREA DS.
0001.00      IDTA1       UDS                                                   
0000.10       * USE IPJ PROMPT TO DEFINE SUBFIELDS OF DATA AREA DS.
0002.00      I                                        1   62AMT1               
0003.00      I                                        7  122AMT2               
0004.00      I                                       13  202SUM                
0005.00      C           *NAMVAR   DEFN           DTA1                         
0006.00      C                                 Z-ADD100       AMT1                         
0007.00      C                                 Z-ADD200.20    AMT2                         
0008.00      C           AMT1           ADD  AMT2      SUM                           
0009.00      C           *LOCK        OUT  DTA1                                   
0010.00      C                                UNLCKDTA1                                   
0011.00      C           SUM             DSPLY                                        
0012.00      C                                 SETON                     LR                
        ****************** End of data ****************************************
                                                                               
                                                                                                                                                               
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle      
 F16=Repeat find       F17=Repeat change          F24=More keys                 



Exception or error handling:
In any application development exception/error handling plays a very important role. The errors can occur at any time while running the application program. Therefore, you as a programmer should be capable of handling such errors rather than allowing them to abnormally end the program itself. The term exception tells about the study of various error conditions and error handling implies detecting and correcting errors.

RPG/400 provides File Information Data Structure (INFDS), file exception/error subroutine (INFSR), program status data structure and program exception/error subroutine (*PSSR) for exception/error handling.

INFSR is used with *PSSR is used to find file level exception and only program level exception are handle by Program Status Data Structure using *PSSR.

When error code is greater than 99 then user can able to use *PSSR routine. Other wise user have to use simple subroutine to handle the exception.

 These are some error codes which are use to handle the error.

TABLE FILE ERROR CODES
NORMAL CONDITIONS
CODES
MEANING
00000
No exception/error occurred.
00002
Function key used to end display.
00011
End of file on a read (input).
00012
No record found on a CHAIN, SETGT, SETLL operation.
00013
Subfile is full on a write operation.
EXCEPTION-ERROR CONDITIONS
01011
Undefined record type (input record does not match record-identifying indicator).
01021
Tried to write a record that already exists(file being used has unique keys and key duplicate, or attempted to use duplicate relative record number to a subfile).
01031
Match field out of sequence.
01041
Array/table load sequence error.
01051
Excess entries in array/table file.
01071
Numeric sequence error.
01121
No indicator on the DDS keyword for Print Key
01122
No indicator on the DDS keyword for Roll Up Key.
01123
No indicator on the DDS keyword for Roll Down key.
01124
No indicator on the DDS keyword for Clear Key.
01125
No indicator on the DDS keyword for Help Key.
01126
No indicator on the DDS keyword for Home Key.
01201
Record mismatch detected on input.
01211
I/O operation to a closed file.
01215
OPEN issued to a file already open.
01216
Error on an implicit OPEN/CLOSE operation.
01217
Error on an explicit OPEN/CLOSE operation.
01218
Record already locked.
01221
Update operation attempted without a prior read.
01231
Error on SPECIAL file.
01235
Error in PRTCTL space or skip entries.
01241
Record number not found. (Record number specified in record address file is not present in file being processed.)
01251
Permanent I/O error occurred.
01255
Session or device error occurred. Recovery may be possible.
01261
Attempt to exceed maximum number of acquired devices.
01281
Operation to un acquired device.
01282
Job ending with controlled option.
01285
Attempt to acquire a device already acquired.
01286
Attempt to open shared file with SAVDS or IND options
01287
Responses to indicators overlap IND indicators.
01299
Other I/O error detected.
01331
Wait time exceeded for READ from WORKSTN file.
Program status Codes
NORMAL CONDITIONS
CODES
MEANING
00000
No exception error occurred.
00001
Called program returns with the LR indicator on.

EXCEPTION-ERROR CONDITIONS

CODES
MEANING
00100
Value out of range for string operation
00101
Negative square root.
00102
Divide by zero
00121
Invalid array index.
00122
OCCUR outside of range.
00123
RESET attempted during initialization subroutine.
00202
Called program failed, halt indicator (H1 through H9) not on.
00211
Program specified on CALL or FREE not found
00221
Called program tried to use a parameter not passed to it.
00231
Called program returned with halt indicator on.
00232
Halt indicator on in this program.
00233
Halt indicator on when RETRN operation run.
00299
RPG formatted dump failed.
00333
Error on DSPLY operation.
00401
Data area specified on IN or OUT not found
00402
*PDA not valid for non-restart job.
00411
Data area types or lengths do not match.
00412
Data area not locked for output
00413
Errors on IN or OUT operation.
00414
User not authorized to use data area.
00415
User not authorized to change data area.
00421
Error on UNLCK operation
00431
Data area previously locked by another program.
00432
Data area locked by program in the same process.
Character field not entirely enclosed by SO and SI.
00907
Decimal data error (invalid digit or sign).
00970
The level numbers of the generating compiler and the run-time subroutines do not match.
Virtual storage exceeded.
09998
Internal failure in RPG compiler or in run-time subroutines.
09999
Program exception in system routine.
The following reserved words define symbolic locations within the file information data structure (INFDS) and the program status data structure.

  1. (*FILE)
  2. (*OPCODE)
  3. (*PARMS)
  4. (*PROGRAM)
  5. (*RECORD)
  6. (*ROUTINE)
  7. (*STATUS)
See the following example where we use *STATUS opcode with INFDS. 
File- Information Data Structure (INFDS)
The purpose of a file information data structure is to identify exception/error for a file in the program. It must be unique for each file. A file information data structure contains predefined subfields that give information about the type of exception or error that occurred, the operation that the program was running when the exception/error occurred and the status of various conditions. The INFDS data structure is specified either on the file description line itself or on a continuation line for that file on position 54-59 (option entry) and position 60-67 (here the name of entry is entry only). Option entry is used to write INFDS keyword and field entry is used for writing the name of the file information data structure.
Following example shows how to handle duplicate record error.
Step 1-
 Following is the Physical File.

 SEU==>                                                                  EMPMST
 FMT PF .....A..........T.Name++++++RLen++TDpB......Functions++++++++++++++++++
        *************** Beginning of data *************************************
0001.00      A                                       UNIQUE                     
0002.00      A          R EMPMSTR                                               
0003.00      A             EMPNO          3S 0                                  
0004.00      A             ENAME        10A                                    
0005.00      A             BSAL              7S 0                                   
0006.00      A             DEPT              7A         DFT('A')                   
0007.00      A             CITY              5A         DFT('PUNE')                
0008.00      A         K EMPNO                                                 
        ****************** End of data ****************************************
                                                                               
                                                                                                                                                    
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle      
 F16=Repeat find       F17=Repeat change          F24=More keys                


Following is the display file

Columns . . . :    1  71            Edit                         PB01U01D/MYSCR
 SEU==>                                                                    MAIN
 FMT A* .....A*. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 
        *************** Beginning of data *************************************
0000.10      A*%%TS  SD  20040928  130438  PB01U01     REL-V5R2M0  5722-WDS    
0000.20      A*%%EC                                                            
0000.30      A                                      DSPSIZ(24 80 *DS3)         
0000.40      A          R MAIN1                                                
0000.50      A*%%TS  SD  20040928  130438  PB01U01     REL-V5R2M0  5722-WDS    
0000.60      A                                      CF03(03)                   
0000.70      A                                  3 35'INPUT'                    
0000.80      A                                  7 24'EMPNO'                    
0000.90      A                                 15 29'F3-TOEXIT'                
0001.00      A            EMPNO     R        B  7 33REFFLD(EMPMSTR/EMPNO PB01U01D/
0001.10      A            MSG           20   O 12 33                           
        ****************** End of data ****************************************
                                                                                
                                                                               
                                                                               
                                                                                
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle      
 F16=Repeat find       F17=Repeat change          F24=More keys                 


Above is the DDS(Data description specification) for display file and layout of this screen ‘MAIN’  is as follows.


                                                       SIMPLE INPUT SCREEN                                      

                                                                            
                                                                                                                                                       
                    EMPNO    999-                                           
                                                                            
                                                                           
                             OOOOOOOOOOOOOOOOOOOO                           
                                                                             
                                                                            
                         F3-TOEXIT                                          
                                                                             
                                                                            
                                                                                                                                                     
                                                                             
        
                                                                    
Step 3: start with RPG.
Define physical file with information data structure.

Columns . . . :    1  71            Edit                         PB01U01S/MYRPG
 SEU==>                                                                  EXCEP1
 FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++Entry+A....U
        *************** Beginning of data *************************************
0001.00      FEMPMST     IF  E                    DISK         KINFDS DS    A     




Prompt type . . .   FX      Sequence number . . .  0001.00                    
                                                                               
               File        File          End of                   File         
 Filename      Type     Designation       File      Sequence     Format        
 EMPMST         I           F                                      E           
  Mode of           Record                                                      
 Processing      Address Type       Device       Continuation                  
                                    DISK              K                        
                        File            File                                    
  Exit     Entry       Addition        Condition                               
 INFDS      DS             A                                                    
                                                                                
 F3=Exit   F4=Prompt   F5=Refresh          F11=Previous record                 
 F12=Cancel            F23=Select prompt   F24=More keys                       
  
                                                                             
Above is ‘F’ specification where we define ‘INFDS’. We define ‘EMP’ physical file. Now we will move our control to SR1 subroutine for that we have to write the following information in ‘IPFK’ or ‘IPFC’ prompt.

Entries to Define and Name a File Information Data Structure.


Position


 Entry

 6

F

7-52

Blank (if the information is specified on a separate continuation line)


53


K (indicates a continuation line)

54-59


INFDS (identifies this data structure as the file information data structure)


60-65


Name of the file information data structure



Columns . . . :    1  71            Edit                         PB01U01S/MYRPG
 SEU==>                                                                  EXCEP1
 FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++Entry+A....U
        *************** Beginning of data *************************************
0001.00      FEMPMST  IF  E                    DISK         KINFDS DS    A     
0002.00      F                                              KINFSR SR1         



 Prompt type . . .   FC      Sequence number . . .  0002.00                    
                                                                               
  External        Record Number                                                 
 Record Name          Field       Continuation     Option     Entry            
                                       K           INFSR      SR1              
                                                                                
 F3=Exit   F4=Prompt   F5=Refresh          F11=Previous record                 
 F12=Cancel            F23=Select prompt   F24=More keys                       
                                                                                


Now we will insert workstation file then define data structure having name ‘DS’ using ‘IPDS’ prompt.
 IPDS:


DS Specification line is broken into following parts


Positions
Name
Entry
1-2
Page
Page number
3-5
Line
Line number
6
Form type
1
7-12
Data structure name
Blank
Data structure name
13-16

Blank
17
External description
Blank
E
18
Option
Blank
I
S
U
19-20
Record identifying indictors
DS
21-30
External file name
External name or data
Structure
31-43

Blank
44-47
Occurrences
Blank
1-9999
48-51
Data structure length
Blank
1-9999
52-74

Blank
75-80
comments
Optional


Record name: Type the external name of the record format.

Record Identifying indicators:  Type a value from 01 to 99 to represent a general indicator. 

Columns . . . :    1  71            Edit                         PB01U01S/MYRPG
 SEU==>                                                                  EXCEP1
 FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++Entry+A....U
        *************** Beginning of data *************************************
0001.00      FEMPMST  IF  E                    DISK         KINFDS DS    A     
0002.00      F                                              KINFSR SR1         
0003.00      FMAIN    CF  E                    WORKSTN                         
0004.00      IDS          DS                                                   


  

 Prompt type . . .   DS      Sequence number . . .  0004.00                    
                                                                                
 Data Struct                               External                            
    Name       Number    Option    DS     File Name     Occurs    Length       
   DS                              DS                                           
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh          F11=Previous record                 
 F12=Cancel            F23=Select prompt   F24=More keys                        
                                                                               
 Then insert field name ‘AA’ and ‘*STATUS’ at 52 position as shown below using IPJX prompt.

IPJX:

Positions
Name
Entry
Positions
Name
Entry
7-20

Blank
21-30
External field name
Field name
31-52

Blank
53-58
RPG field name
Field name
59-60
Control level
Blank
L1-L9
61-62
Match fields
Blank
M1-M9
63-64

Blank
65-70
Field indictors
Blank
01-99
H1-H9
U1-U8
RT
71-74

Blank
75-80
Comments
Optional


Columns . . . :    1  71            Edit                         PB01U01S/MYRPG
 SEU==>                                                                  EXCEP1
 FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++Entry+A....U
        *************** Beginning of data *************************************
0001.00      FEMPMST  IF  E                    DISK         KINFDS DS    A     
0002.00      F                                              KINFSR SR1         
0003.00      FMAIN    CF  E                     WORKSTN                         
0004.00      IDS          DS                                                   
0005.00      I                                     *STATUS  AA                 



 Prompt type . . .   JX      Sequence number . . .  0005.00                    
                                                                               
 External           Field         Control        Matching                      
 Field Name         Name           Level          Fields                        
                    AA                                                         
 Positive        Negative        Zero /                                        
  Field           Field          Blank                                          
                                                                               
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh          F11=Previous record                 
 F12=Cancel            F23=Select prompt   F24=More keys                       
    

Now simply insert message ‘Duplicate’ using  ‘IPN’ prompt and do the coding as given below.


Columns . . . :    1  71            Edit                          PB01U01S/MYRPG
 SEU==>                                                                  EXCEP1
 FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++Entry+A....U
        *************** Beginning of data *************************************
0001.00      FEMPMST  IF  E                    DISK         KINFDS DS    A     
0002.00      F                                              KINFSR SR1         
0003.00      FMAIN    CF  E                    WORKSTN                         
0004.00      IDS          DS                                                   
0005.00      I                                     *STATUS  AA                 
0006.00      I              'DUPLICATE'           C         MSG1               
0007.00      C                                 READ EMPMSTR                  50            
0008.00      C                  *IN03     DOWEQ*OFF                                   
0009.00      C                   EE          TAG                                         
0010.00      C                                  EXFMTMAIN1                                   
0011.00      C    03                          LEAVE                                       
0012.00      C                                 WRITEEMPMSTR                                
0013.00      C                                 CLEARMSG                                     
0014.00      C                                 ENDDO                                       
0015.00      C                                 SETON                     LR                
0016.00       ************************************************************* 
0017.00      C                  SR1       BEGSR                                       
0018.00      C                  AA         IFEQ 01021                                  
0019.00      C                                MOVE MSG1      MSG                          
0020.00      C                                GOTO EE                                     
0021.00      C                                ENDIF                                       
0022.00      C                                ENDSR                                       
 ****************** End of data ****************************************
                                                                                     
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle      
 F16=Repeat find       F17=Repeat change          F24=More keys                
                                                                               


Above example shows you how to handle the file related error.

Now in next example see how to handle the program related error.
Program Exception/Errors
The exception/errors that occurred during program execution time are known as program exception error. i.e. an error on a CALL operation code, division by zero, Square root of a negative number etc. Therefore if you are doing above operations in a program it’s always a good programming practice to provide logic to handle these errors.

A program-status data structure, identified by an S in position 18 of the data structure statement, provides program exception/error information to the program. You can handle these errors in one of following ways.

  • Use error indicator in position 56-57 of those operations that allows such an entry. If error occurs than the indicator sets on. You can determine the action to be taken by testing the indicator for on condition.

  • A program exception/error subroutine *PSSR can be used. This subroutine will automatically receive control when a program exception /error occurs. Information regarding the program exception/error is made available through a program status data structure that will be described in next section

  • If the error indicator or the program exception/error subroutine is not present than the RPG/400 default error handler handles program exception/error.

We use a subroutine  ‘*PSSR’ to handle the error, when error occurs program starts execution of this subroutine.
            *PSSR               BEGSR
                                    -----------
                                    -----------
                                    -----------
                                    ENDSR
Above is the syntax for subroutine.


So how to implement it in our program is in the following example.


Columns . . . :    1  71            Edit                         PB01U01S/MYRPG
 SEU==>                                                                 EXMP10
 FMT *  ..... *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 
        *************** Beginning of data *************************************
0000.01       * PROGRAM FOR PROGRAM STATUS DS DEFINE MESSAGE USING IPN PROMPT.                                 
0001.00      I              'OBJECT NOT FOUND'    C         MSG1     
0001.01       *  DEFINE DS USING IPDS PROMPT.    
0002.00      IDS2        SDS                      
0001.01       *  DEFINE FIELD NAME AND ‘*STATUS’ AT 52 POSITION USING IPJX.    
0003.00      I                                     *STATUS  ST                 
0004.00      C                       CALL 'RLG'                                  
0005.00      C            A        TAG                                         
0006.00      C                       SETON                     LR      
0006.01       * SUBROUTINE FOR ERROR HANDLING
0007.00      C       *PSSR     BEGSR                                       
0008.00      C        ST           IFEQ 211                                     
0009.00      C        MSG1     DSPLY                                       
0010.00      C                       GOTO A                                      
0011.00      C                       ENDIF                                       
0012.00      C                       ENDSR                                       
        ****************** End of data ****************************************
                                                                                                                                                             
                                                                               
 F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F10=Cursor   F11=Toggle      
 F16=Repeat find       F17=Repeat change          F24=More keys                
                                         (C) COPYRIGHT IBM CORP. 1981, 2002.   


In above example we use to find out the whether the object of program is existing or not. If program object is exist then ‘Call’ command execute or call that particular program. If the object of the program does not exist then error message will gets displayed ‘Object Not Found’. So the code for that checking is 211.











  

No comments:

Post a Comment