| Using INFDS with INFSR catch file exception in RPG AS400 |
File Information Data Structure (INFDS)
You can read more about INFDS in the following article.
Using a File Error Subroutine (INFSR)
We can write an INFSR subroutine to handle the file errors or exceptions. When a file error occurs following things happen.
Example of Using INFDS with INFSR to catch file exception in RPG AS400
Here we will be going to perform the operation on a closed file. We will write code in Fixed format, /Free format, and Fully free format.
RPG Code in Fixed format for Coding File Information Data Strucure (INFDS) in RPGLE AS400
*_________________________________________________________________________
* The INFDS contains the following feedback information:
* File Feedback (length is 80) <-- This video talks about
* Open Feedback (length is 160)
* Input/Output Feedback (length is 126)
* Device Specific Feedback (length is variable)
* Get Attributes Feedback (length is variable)
*_________________________________________________________________________
FCLPF1A IF E K DISK INFDS(DS1) INFSR(*pssr) usropn
D DS1 DS
* File Feedback information (starts in position 1 and ends in position 80)
* file name
* record process
* last operation
* status code (>99 is exception)
* RPG IV routine
D FILE *FILE * File name
D OPEN_IND 9 9N * File open
D EOF_IND 10 10N * File at eof
D STATUS *STATUS * Status code
D OPCODE *OPCODE * Last opcode
D ROUTINE *ROUTINE * RPG Routine
D LIST_NUM 30 37 * Listing line
D SPCL_STAT 38 42S 0 * SPECIAL status
D RECORD *RECORD * Record name
D MSGID 46 52 * Error MSGID
D SCREEN *SIZE * Screen size
D NLS_IN *INP * NLS Input
D NLS_OUT *OUT * NLS Output
D NLS_MODE *MODE * NLS Mode?
*variable
D uwrollno S 5P 0 INZ(3)
D returncode S 6A
C OPEN CLPF1A
C CLOSE CLPF1A
C uwrollno CHAIN CLPF1A
C IF %FOUND
C EVAL uwrollno = uwrollno + 1
C ELSE
C EVAL uwrollno = 0
C ENDIF
C EVAL *INLR = *ON
C *PSSR BEGSR
C IF STATUS = 01211
C EVAL returncode = ' '
C EVAL returncode = '*CANCL'
C EVAL returncode = '*GETIN'
C ENDIF
C ENDSR returncode
RPG Code in /Free format for Coding File Information Data Strucure (INFDS) in RPGLE AS400
FCLPF1A IF E K DISK INFDS(DS1) INFSR(*pssr) usropn
D DS1 DS
D FILE *FILE * File name
D OPEN_IND 9 9N * File open
D EOF_IND 10 10N * File at eof
D STATUS *STATUS * Status code
D OPCODE *OPCODE * Last opcode
D ROUTINE *ROUTINE * RPG Routine
D LIST_NUM 30 37 * Listing line
D SPCL_STAT 38 42S 0 * SPECIAL status
D RECORD *RECORD * Record name
D MSGID 46 52 * Error MSGID
D SCREEN *SIZE * Screen size
D NLS_IN *INP * NLS Input
D NLS_OUT *OUT * NLS Output
D NLS_MODE *MODE * NLS Mode?
/Free
open clpf1a;
close clpf1a;
chain uwrollno clpf1a;
If %found;
uwrollno = uwrollno + 1;
Else;
uwrollno = 0;
Endif;
*INLR = *ON;
// Status code
// 00000 --> No Exception/Error
// 00011 --> End of file on a READ (input)
// 01021 --> tried to write duplicate record on unique keys
// 01211 --> I/O opeartion to a closed file
// 01215 --> OPEN issued to a file already opened
// 01218 --> Record already locked
// 01221 --> update operation attempted without a prior read
// 01255 --> session or device error occurred
//________________________________________________________________
Begsr *pssr;
If STATUS = 01211;
// returncode = ' ';
//returncode = '*CANCL';
returncode = '*GETIN';
EndIf;
endsr returncode;
//________________________________________________________________
/End-Free
RPG Code in fully Free format for Coding File Information Data Strucure (INFDS) in RPGLE AS400
**FREE
DCL-F CLPF1A DISK(*EXT) USAGE(*INPUT) INFDS(DS1) keyed INFSR(*pssr) usropn ;
DCL-DS DS1;
FILE *FILE; // File name
OPEN_IND IND POS(9); // File open
EOF_IND IND POS(10); // File at eof
STATUS *STATUS; // Status code
OPCODE *OPCODE; // Last opcode
ROUTINE *ROUTINE; // RPG Routine
LIST_NUM CHAR(8) POS(30); // Listing line
SPCL_STAT ZONED(5) POS(38); // SPECIAL status
RECORD *RECORD; // Record name
MSGID CHAR(7) POS(46); // Error MSGID
SCREEN *SIZE; // Screen size
NLS_IN *INP; // NLS Input
NLS_OUT *OUT; // NLS Output
NLS_MODE *MODE; // NLS Mode
END-DS;
dcl-s uwrollno packed(5:0) inz(3);
dcl-s returncode char(6);
open clpf1a;
close clpf1a;
chain uwrollno clpf1a;
If %found;
uwrollno = uwrollno + 1;
Else;
uwrollno = 0;
Endif;
*INLR = *ON;
Begsr *pssr;
If STATUS = 01211;
// returncode = ' ';
//returncode = '*CANCL';
returncode = '*GETIN';
EndIf;
endsr returncode;
Related Post
Read also :
- Data Structure and Types of DS in RPG AS400
- Using a Data Structure to subdivide the field in RPG AS400
- Using a Data Structure to group fields in RPG AS400
- Externally Described Data Structure in RPG AS400
- Using EXTFLD to code Externally Described DS in RPG AS400
- Using PREFIX to rename all fields in an external data structure in RPG AS400
- Define an externally-described data structure using the LIKEREC keyword in RPG AS400
- Difference between LIKEREC and EXTNAME keyword in RPG AS400
- Multiple Occurrence Data Structure in RPG AS400
- Data Area Data Structure in RPG AS400
- *LDA Local data area data structure in RPG AS400
- File information data structures (INFDS) in RPG AS400
- Indicator data structure in RPG AS400
- Program Status Data Structure in RPG AS400
- Using keywords QUALIFIED, LIKEDS, and DIM with data structures
- Array Data Structures in RPG AS400
- Defining Data Structure Parameters in a Prototype or Procedure Interface
- PSDS with *PSSR to catch program exceptions in RPG AS400