ILE COBOL – Error Handling
Record Lock Error
A record lock error in IBM i ILE COBOL occurs when your program tries to read or update a database record that is currently locked by another job or process. In IBM i COBOL, it usually triggers a file status of 9D (Record locked) or CPF5027 (System exception message ID).
Example:
PROCESS APOST, XREF, NOPRTCORR, FLAG(29).
IDENTIFICATION DIVISION.
*-----------------------*
PROGRAM-ID. RECORDLOCK.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
*---------------------*
CONFIGURATION SECTION.
*----------------------*
SOURCE-COMPUTER. IBMi-Series.
OBJECT-COMPUTER. IBMi-Series.
SPECIAL-NAMES. PROGRAM STATUS IS WS-PSDS.
INPUT-OUTPUT SECTION.
*---------------------*
FILE-CONTROL.
SELECT ACCOUNT-FILE
ASSIGN TO DATABASE-ACCTPF
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS EXTERNALLY-DESCRIBED-KEY
WITH DUPLICATES
FILE STATUS IS ACCOUNT-FILE-STATUS.
DATA DIVISION.
*--------------*
FILE SECTION.
*-------------*
FD ACCOUNT-FILE LABEL RECORDS ARE STANDARD.
01 ACCOUNT-REC. COPY DD-ACCTPFR OF ACCTPF.
WORKING-STORAGE SECTION.
*------------------------*
01 ACCOUNT-FILE-STATUS PIC X(02).
01 WS-PSDS.
05 FILLER PIC X(46).
05 WS-EXCP-ID PIC X(07).
05 FILLER PIC X(41).
PROCEDURE DIVISION.
*-------------------*
MAINLINE.
OPEN I-O ACCOUNT-FILE.
IF ACCOUNT-FILE-STATUS NOT = '00'
DISPLAY 'Error in opening file for ACCOUNT-FILE.'
ACCOUNT-FILE-STATUS WS-EXCP-ID
GOBACK
END-IF.
* Read the first record of the dynamically for the indexed file
READ ACCOUNT-FILE FIRST RECORD.
IF ACCOUNT-FILE-STATUS NOT = '00'
IF WS-EXCP-ID = 'CPF5027' OR ACCOUNT-FILE-STATUS = '9D' or ‘90’
DISPLAY 'Record Lock Error for ACCOUNT-FILE.'
ACCOUNT-FILE-STATUS
DISPLAY 'Exception ID:’ WS-EXCP-ID
ELSE
DISPLAY 'Error in READ operation'
ACCOUNT-FILE-STATUS WS-EXCP-ID
END-IF
END-IF.
*
CLOSE ACCOUNT-FILE.
IF ACCOUNT-FILE-STATUS NOT = '00'
IF ACCOUNT-FILE-STATUS NOT = '00'
DISPLAY 'Error in closing file for ACCOUNT-FILE.'
ACCOUNT-FILE-STATUS WS-EXCP-ID
END-IF.
GOBACK.
Steps to re-create record lock error:
- Open two different IBM i sessions.
Execute the below CL command in debug mode in first IBM i session.
- CALL PGM(TESTLAB/RECORDLOCK)
- Process the first READ statement and pause at next record for status check.
- Last statement has been processed with I-O mode and with no lock, so record is locked.
-
Execute the below CL command in debug mode in the second IBM i session.
- CALL PGM(TESTLAB/RECORDLOCK)
-
For processing READ statement, it will be locked and after 60 seconds(default) it will throw error.
- FILE STATUS = ‘9D’
- EXCEPTION-ID = ‘CPF5027’
Display Module Source
Program: RECORDLOCK Library: TESTLAB Module: RECORDLOCK
36 PROCEDURE DIVISION.
37 *-------------------*
38 MAINLINE.
39 OPEN I-O ACCOUNT-FILE.
40 IF ACCOUNT-FILE-STATUS NOT = '00'
41 DISPLAY 'Error in opening file for ACCOUNT-FILE.'
42 ACCOUNT-FILE-STATUS WS-EXCP-ID
43 GOBACK
44 END-IF.
45
46 * Read the first record of the dynamically for the indexed fi
47 READ ACCOUNT-FILE FIRST RECORD.
48 IF ACCOUNT-FILE-STATUS NOT = '00'
49 IF WS-EXCP-ID = 'CPF5027' OR ACCOUNT-FILE-STATUS = '9D'
50 DISPLAY 'Record Lock Error for ACCOUNT-FILE.'
More...
Debug . . .
F3=End program F6=Add/Clear breakpoint F10=Step F11=Display variable
F12=Resume F17=Watch variable F18=Work with watch F24=More keys
Programmatical fix to avoid ABEND:
1. Identify the error using file status and take further steps accordingly.
To detect a record lock, programmer must define and check the FILE STATUS clause in code.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT MY-FILE
ASSIGN TO DATABASE-MYFILE
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS EXTERNALLY-DESCRIBED-KEY
FILE STATUS IS WS-FILE-STATUS.
WORKING-STORAGE SECTION.
01 WS-FILE-STATUS PIC X(02).
When a record lock occurs:
- File Status = ‘9D’
2. Identify the error using program data structure and take further steps accordingly.
To detect a record lock, programmer must define and check the EXCEPTION ID field in code.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBMi-Series.
OBJECT-COMPUTER. IBMi-Series.
SPECIAL-NAMES. PROGRAM STATUS IS WS-PSDS.
WORKING-STORAGE SECTION.
01 WS-PSDS.
05 FILLER PIC X(46).
05 WS-EXCP-ID PIC X(07).
05 FILLER PIC X(41).
When a record lock occurs:
- Exception ID = ‘CPF5027’
3. Handle the error in code
Handle the file status or exception ID while executing read operation. Use explicit error handling during READ or START operations.
Example: Imperative Statement Handling
READ MY-FILE INVALID KEY
DISPLAY "Record not found"
NOT INVALID KEY
IF WS-FILE-STATUS = "9D" OR WS-EXCP-ID = ‘CPF5027’
DISPLAY "Record is locked by another user!"
PERFORM 999-END-LOGIC
END-IF
END-READ.
4. Handle the error via Declarative Error Handling (USE AFTER STANDARD ERROR)
PROCEDURE DIVISION.
DECLARATIVES.
MY-FILE-ERROR SECTION.
USE AFTER STANDARD EXCEPTION PROCEDURE ON MY-FILE.
IF WS-FILE-STATUS = "9D"
DISPLAY "Record locked. Retrying..."
*> --- Logic to log details & generated required dump ---
END-IF.
END DECLARATIVES.
5. Avoiding unintentional locks in codes
By default, opening a file in I-O (Input-Output) mode locks every record having START or READ operation. So that you can update it. If you only need to look at the data, you can prevent locking.
-
Read Without Lock (No-Lock Read):
If a file is opened in I-O mode but user does not intend to update or delete this specific record, use the WITH NO LOCK phrase.
READ MY-FILE RECORD WITH NO LOCK READ MY-FILE NEXT RECORD WITH NO LOCK START MY-FILE KEY >= EXTERNALLY-DESCIBED-KEY WITH NO LOCK
-
Manage Lock Wait Timeout
When a record is locked, IBM i will make program wait for a specific number of seconds before throwing the error. Programmers can control this behavior:
System/File Level: The default wait time is determined by the WAITRCD parameter on the physical file (CHGPF) or logical file (CHGLF).
Job Level: Override the wait time for your entire job or program execution using the Control Language (CL) command before calling your COBOL program:
OVRDBF FILE(MYFILE) WAITRCD(60) OVRDBF FILE(MYFILE) WAITRCD(*IMMED)
Troubleshooting Active locks
If a program is currently stuck or failing due to a lock, use these IBM i system commands to diagnose the issue:
-
WRKOBJLCK OBJ(MYLIB/MYFILE) OBJTYPE(*FILE)
Shows which jobs hold locks on the file.
-
DSPRCDLCK FILE(MYLIB/MYFILE) MBR(*FIRST) RCDNBR(*ALL)
Displays exactly which record (by relative record number) is locked and which job holds it.
-
Using below query
SELECT * FROM QSYS2.RECORD_LOCK_INFO WHERE SYSTEM_TABLE_SCHEMA = ‘MYLIB’ AND SYSTEM_TABLE_NAME = ‘MYFILE’
-
WRKJOB (Option 11)
Look at the job log of failing program to find the underlying CPF5027 message, which specifies the job name holding the conflicting lock.
File Level Check Error
A file level check error, on an IBM i system, occurs when the record format identifier of a physical, logical, or display file does not match the format identifier embedded within the program attempting to open it. This triggers the escape message CPF4131, which halts the application during the file open cycle.
Additional Message Information
Message ID . . . . . . : RNQ1216 Severity . . . . . . . : 99
Message type . . . . . : Inquiry
Date sent . . . . . . : 07/16/26 Time sent . . . . . . : 04:55:39
Message . . . . : Error message CPF4131 appeared during OPEN for file INPUT
(C S D F).
Cause . . . . . : COBOL procedure Z01DUMMY01 in program ASHWANI/Z01DUMMY01
received the message CPF4131 while performing an implicit OPEN operation on
file INPUT. The actual file is INPUT.
Recovery . . . : Check the job log for a complete description of message
CPF4131, and contact the person responsible for program maintenance. If the
file has a device type of SPECIAL, there may be no message in the job log.
Why this CPF4131 abend occurs?
In IBM i DB2, when a programmer creates a physical file using the CRTPF command, the system generates a unique record format level identifier. This identifier is based on the number of fields, their order, names, data types, and lengths. This record format identifier can be viewed using CL command DSPFD in the ‘Record Format List’ section.
Record Format List
Record Format Level
Format Fields Length Identifier
ORDERR 40 503 4F1D21C628A22
Text . . . . . . . . . . . . . . . . . . . :
Total number of formats . . . . . . . . . . : 1
Total number of fields . . . . . . . . . . . : 40
Total record length . . . . . . . . . . . . : 503
Similarly, when a programmer creates a logical file using CRTLF, a unique record format identifier is generated. Simple and join logical files have one identifier, while multi-format logical files have one identifier for each record format.
Record Format List
Record Format Level
Format Fields Length Identifier
ORDHDRPFR 9 61 3395B050F5234
Text . . . . . . . . . . . . . . . . . . . :
ORDDTLPFR 8 106 5128F15B369AE
Text . . . . . . . . . . . . . . . . . . . :
Total number of formats . . . . . . . . . . : 2
Total number of fields . . . . . . . . . . . : 17
Total record length . . . . . . . . . . . . : 167
Similarly, display files (DSPF) and printer files (PRTF) also get a unique identifier for each record format.
Record Format List
Record Format Level Format
Format Fields Length Identifier Type
SF 7 73 17CBBC2333052 SFL
Associated format . . . . . . . . . . . . : S1
Text . . . . . . . . . . . . . . . . . . . :
S1 6 50 16C4B9572CF56 SFLCTL
Associated format . . . . . . . . . . . . : SF
Text . . . . . . . . . . . . . . . . . . . :
SD 5 54 1228DD8D72E29 Normal
Text . . . . . . . . . . . . . . . . . . . :
T1 3 8 090409466B536 Normal
Text . . . . . . . . . . . . . . . . . . . :
Total number of formats . . . . . . . . . . : 4
Total number of fields . . . . . . . . . . . : 21
Total record length . . . . . . . . . . . . : 185
No matter what value developers (system default =*YES) set for LVLCHK while creating files (CRTPF, CRTLF, CRTDSPF, CRTPRTF), the system always generates a record format identifier.
When *LVLCHK(YES) is used and a program (ILE COBOL or SQLCBLLE) is compiled, the system creates a unique code based on the file structure—like field names, order, data types, and lengths. This code is stored in the program.
If the file is changed later (for example, adding a field, changing type or length, or rearranging fields), a new identifier is created.
If a programmer tries to run an old program without recompiling it, the program’s stored identifier will not match the file’s new identifier. Because of this mismatch, the program will crash with CPF4131.
When *LVLCHK(NO) is used and a program (ILE COBOL or SQLCBLLE) is compiled, the system passes READ operation but gives error CPF4131 in WRITE and UPDATE operations.
-
Identify & recompile the affected Program (Recommended)
Identify all programs affected by new file layouts. The safest and most permanent solution is to recompile the program. This forces the program to inherit the file’s newly generated format identifier.
-
Temporary Workaround: Bypass Level Checking (Not Recommended)
If a non-breaking modification (such as adding a field to the very end of the file that the program doesn’t interact with) and needs an immediate fix without recompiling, the programmer can bypass the check.
Via File Override (Runtime): Execute an override command before calling the program: OVRDBF FILE(FILENAME) LVLCHK(*NO)
Via File Modification (Object level): Permanently change the file’s attribute using the IBM i Change Physical File (CHGPF) command: CHGPF FILE(LIBRARY/FILENAME) LVLCHK(*NO)
Error Handling – Introduction
Error handling is a method used to handle potential runtime errors by sending messages to the user or program, or by routing the program flow so that the program abend can be skipped.
COBOL400:
Native COBOL uses 2 basic error handling methods:
- File status codes for I/O file operations.
- In-line phrases such as AT END, INVALID KEY, and NO LOCK for I/O file operations. Also, in-line phrases such as ON SIZE, ON OVERFLOW, and ON EXCEPTION for string, arithmetic, and CALL operations respectively.
- Declarative Section.
ILE COBOL:
ILE COBOL utilizes IBMi features to access detailed run-time and exception information. There are two clauses that helps in gathering run-time information:
- File status codes for I/O file operations.
- In-line phrases such as AT END, INVALID KEY, and NO LOCK for I/O file operations. Also, in-line phrases such as ON SIZE, ON OVERFLOW, and ON EXCEPTION for string, arithmetic, and CALL operations respectively.
- Declarative Section.
Error Handling Techniques in COBOL400 or ILE COBOL
There are 5 methods in which error handling mechanism in ILE COBOL can be categorised:
- COBOL Verb in-line phrases
- File Status for I/O operations
- Declarative Sections
- PROGRAM STATUS Clause in Special-Name paragraph
- OPEN-FEEDBACK Clause in Special-Name paragraph
- I-O-FEEDBACK Clause in Special-Name paragraph
In-Line Phrases
To handle command-level errors in native COBOL programs, in-line phrases are provided to specific COBOL verbs. These are like:
- Command-level error handling in CL programs using the MONMSG command.
- Command-level error handling in RPGLE programs using the MONITOR, ON-ERROR, and ENDMON opcodes.
Below are the in-line phrases that handles and traps the error:
- Handling errors in string operations (ON OVERFLOW)
- Handling errors in arithmetic operations (ON SIZE ERROR)
- Handling errors in CALL statements (ON EXCEPTION/ON OVERFLOW)
- Handling errors in I/O operations (AT END, INVALID KEY, NO LOCK)
File Status for I/O operations
In native COBOL, the FILE STATUS clause in the FILE-CONTROL paragraph of the INPUT-OUTPUT SECTION of the ENVIRONMENT DIVISION is used to monitor the execution of each input-output operation for a specific file. The FILE STATUS clause is a 2-character string that changes with each file operation and helps in error handling and trapping.
Declarative Sections
In native COBOL, Declaratives are special-purpose sections within the PROCEDURE DIVISION that are used to handle specific conditions or events, such as I/O errors or debugging scenarios. They provide a structured way to define actions to be taken when these conditions arise that are automatically invoked when specific exceptional conditions arise, primarily concerning I/O errors or debugging events.
This eliminates the need to explicitly check file status codes after every I/O operation, streamlining error management, and making the code cleaner and more efficient. It acts as program-level error handling in COBOL programs.
PROGRAM STATUS Clause
In ILE COBOL, the PROGRAM STATUS clause within the SPECIAL-NAMES paragraph of the ENVIRONMENT DIVISION provides error information at runtime. PROGRAM STATUS is a group variable of 94 characters. The exception message ID is contained in characters 47 through 53 (7 characters). Programmers can use this message ID to trap errors and redirect the program flow.
OPEN-FEEDBACK Clause
In ILE COBOL on IBMi, OPEN-FEEDBACK clause within the SPECIAL-NAMES paragraph of the ENVIRONMENT DIVISION are special areas capturing detail information about a file, but only when the file is open. These details can be captured through ACCEPT statement.
I-O-FEEDBACK Clause
In ILE COBOL on IBMi, I-O-FEEDBACK clause within the SPECIAL-NAMES paragraph of the ENVIRONMENT DIVISION are special areas capturing detail information about the last I-O operation on a file, but only when the file is open. These details can be captured through ACCEPT statement.
Error Handling via file status
Native COBOL file status provides information about the result of each file operation executed. The file status changes after every file operation. It consists of 2-digit character and below is the important file status:
- ’00’ – Successful operation.
- ‘10’ – File End-of-file reached during an input operation.
- ‘22’ – Duplicate key in an indexed or relative file having unique constraint.
- ‘23’ – Record not found.
- ‘35’ – File not found in library list.
- ‘41’ – Open operation has been tried on a file which is already opened.
- ‘42’ – File not open – Cannot be closed.
- ‘47’ – Attempted I/O operation on a file when file is not opened.
- ‘90’ – Record lock.
- ‘94’ – Delete operation failed as read operation was not successful.
- ‘9S’ – Delete operation failed as the last record was read with no lock.
1. OPEN and CLOSE verbs
►For COBOL verbs -> OPEN and CLOSE, if file status changes not equal to 0 after operations, then it means file operation fails.
Example 1: OPEN Verb file Status handling
* ---------- * Open Files * ---------- OPEN-FILES-PARA. OPEN INPUT ACCT-FILE OUTPUT ACCTX-FILE. IF WS-ACCT-STATUS NOT = '00' DISPLAY 'ERROR IN OPENING FILE: ACCT-FILE' WS-ACCT-STATUS END-IF. IF WS-ACCTX-STATUS NOT = '00' DISPLAY 'ERROR IN OPENING FILE: ACCTX-FILE' WS-ACCTX-STATUS END-IF. OPEN-FILES-PARA-EXIT. EXIT.
Example 2: CLOSE Verb Status handling
* -----------
* Close files
* -----------
CLOSE-FILES-PARA.
CLOSE ACCT-FILE
ACCTX-FILE.
IF WS-ACCT-STATUS NOT = "00"
DISPLAY 'ERROR IN CLOSING FILE :ACCT-FILE'
WS-ACCT-STATUS
END-IF.//
IF WS-ACCTX-STATUS NOT = "00"
DISPLAY 'ERROR IN CLOSING FILE :ACCTX-FILE'
WS-ACCTX-STATUS
END-IF.
CLOSE-FILES-PARA-EXIT. EXIT.
2. READ NEXT (Sequential Read and dynamic read)/READ FIRST/LAST/PRIOR (Dynamic read)
► For READ NEXT/FIRST/LAST/PRIOR, if status code is not equal to 0 or 10 then file operation is failed.
- ‘0’ – Successful
- ‘10’ – AT END or no records are left to read.
Example 1: READ Verb status handling (Sequential Read)
* -----------------------------
* Read all records sequentially
* -----------------------------
READ-ACCT-PARA.
READ ACCT-FILE NEXT RECORD
IF WS-ACCT-STATUS = "10"
STOP RUN.
ELSE
IF WS-ACCT-STATUS NOT = "00"
DISPLAY 'ERROR IN READING FILE: ACCT-FILE'
WS-ACCT-STATUS
END-IF
END-IF.
Example 2: When access mode is dynamic and first record is read and just after that PRIOR record is read, it reaches status = 10 (BOF – Beginning of file is reached) READ Verb status handling (Dynamic Read)
READ-ACCT-PARA.
READ ACCT-FILE FIRST RECORD
IF WS-ACCT-STATUS = "10"
GO TO READ-ACCT-PARA-EXIT
ELSE
IF WS-ACCT-STATUS NOT = "00"
DISPLAY 'ERROR IN READING FILE: ACCT-FILE'
WS-ACCT-STATUS
END-IF
END-IF.
READ ACCT-FILE PRIOR RECORD
* Check if beginning of file is reached.
IF WS-ACCT-STATUS = "10"
GO TO READ-ACCT-PARA-EXIT
ELSE
IF WS-ACCT-STATUS NOT = "00"
DISPLAY 'ERROR IN READING FILE: ACCT-FILE'
WS-ACCT-STATUS
END-IF
END-IF.
3. READ RECORD
►For READ RECORD, if status code = ‘23’ then file operation fails.
Example 1: READ Verb status handling (Random Read)
READ-ACCT-PARA.
READ ACCT-FILE FIRST RECORD
IF WS-ACCT-STATUS = "10"
GO TO READ-ACCT-PARA-EXIT
ELSE
IF WS-ACCT-STATUS NOT = "00"
DISPLAY 'ERROR IN READING FILE: ACCT-FILE'
WS-ACCT-STATUS
END-IF
END-IF.
READ ACCT-FILE PRIOR RECORD
* Check if beginning of file is reached.
IF WS-ACCT-STATUS = "10"
GO TO READ-ACCT-PARA-EXIT
ELSE
IF WS-ACCT-STATUS NOT = "00"
DISPLAY 'ERROR IN READING FILE: ACCT-FILE'
WS-ACCT-STATUS
END-IF
END-IF.
Output:
RECORD NOT FOUND : ACCT-FILE FS-23
4. REWRITE
►For Sequential file, index, relative file with unique or non-unique constraint, if status code != 0 then file operation fails.
Example 1: REWRITE Verb status handling (After Sequential Read)
READ-ACCT-PARA.
MOVE '04' TO DIVID OF ACCT-FILE.
MOVE '405' TO ACCTNO OF ACCT-FILE.
READ ACCT-FILE RECORD.
IF WS-ACCT-STATUS = "23"
DISPLAY 'RECORD NOT FOUND :ACCT-FILE FS-'
WS-ACCT-STATUS
END-IF.
READ-ACCT-PARA-EXIT. EXIT.
Output:
RECORD NOT FOUND : ACCT-FILE FS-23
Example 2: REWRITE Verb status handling (After Random Read)
READ-ACCT-PARA.
MOVE '04' TO DIVID OF ACCT-FILE.
MOVE '405' TO ACCTNO OF ACCT-FILE.
READ ACCT-FILE RECORD.
IF WS-ACCT-STATUS = "23"
DISPLAY 'RECORD NOT FOUND :ACCT-FILE FS-'
WS-ACCT-STATUS
END-IF.
READ-ACCT-PARA-EXIT. EXIT.
Output:
RECORD NOT FOUND : ACCT-FILE FS-23
5. WRITE
►For Sequential file, index or relative file with non-unique constraint, if status code != 0 then file operation fails.
►For indexed file having unique constraint:
- if status code = ‘22’, then ‘Attempted to write a duplicate record’.
- if status code <> 0, then file operation fails.
Example 1: WRITE Verb status handling (After Sequential Read)
READ-ACCT-PARA.
MOVE '04' TO DIVID OF ACCT-FILE.
MOVE '405' TO ACCTNO OF ACCT-FILE.
READ ACCT-FILE RECORD.
IF WS-ACCT-STATUS = "23"
DISPLAY 'RECORD NOT FOUND :ACCT-FILE FS-'
WS-ACCT-STATUS
END-IF.
READ-ACCT-PARA-EXIT. EXIT.
Output:
RECORD NOT FOUND : ACCT-FILE FS-23
Example 2: WRITE Verb status handling (After Random Read)
READ-ACCT-PARA.
MOVE '04' TO DIVID OF ACCT-FILE.
MOVE '405' TO ACCTNO OF ACCT-FILE.
READ ACCT-FILE RECORD.
IF WS-ACCT-STATUS = "23"
DISPLAY 'RECORD NOT FOUND :ACCT-FILE FS-'
WS-ACCT-STATUS
END-IF.
READ-ACCT-PARA-EXIT. EXIT.
Output:
RECORD NOT FOUND : ACCT-FILE FS-23
5. DELETE
►For Sequential file, index or relative file with non-unique constraint, if status code != 0 then file operation fails.
- if status code = ‘94’, then ‘Delete operation attempted before read operation’
- if status code = ‘9S’, then ‘Delete operation attempted on read with no lock record’
- if status code != 0, then file operation fails.
Example 1: DELETE verb status handling (When Status code = ‘94’)
IDENTIFICATION DIVISION.
*------------------------*
PROGRAM-ID. DELETEOP01.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
*---------------------*
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBMi-Series.
OBJECT-COMPUTER. IBMi-Series.
INPUT-OUTPUT SECTION.
*---------------------*
FILE-CONTROL.
SELECT ACCT-FILE
ASSIGN TO DATABASE-ACCTPF
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS EXTERNALLY-DESCRIBED-KEY
WITH DUPLICATES
FILE STATUS IS WS-ACCT-STATUS.
DATA DIVISION.
*--------------*
FILE SECTION.
FD ACCT-FILE
LABEL RECORDS ARE STANDARD.
01 ACCT-RCD.
COPY DDS-ALL-FORMATS OF ACCTPF
WITH PREFIX BY 'A-'.
WORKING-STORAGE SECTION.
*-----------------------*
77 WS-ACCT-STATUS PIC XX.
*------------------*
PROCEDURE DIVISION.
*------------------*
MAINLINE.
PERFORM OPEN-FILES-PARA
THRU OPEN-FILES-PARA-EXIT.
PERFORM READ-ACCT-PARA
THRU READ-ACCT-PARA-EXIT.
PERFORM CLOSE-FILES-PARA
THRU CLOSE-FILES-PARA-EXIT.
STOP RUN.
* ----------
* Open Files
* ----------
OPEN-FILES-PARA.
OPEN I-O ACCT-FILE.
IF WS-ACCT-STATUS NOT = '00'
DISPLAY 'ERROR IN OPENING FILE: ACCT-FILE' WS-ACCT-STATUS
END-IF.
OPEN-FILES-PARA-EXIT. EXIT.
* ------------------------------
* Read sequentially with no lock
* ------------------------------
READ-ACCT-PARA.
DELETE ACCT-FILE RECORD
IF WS-ACCT-STATUS NOT = 0
DISPLAY 'ERROR IN DELETE OPERATION: ' WS-ACCT-STATUS
END-IF
READ-ACCT-PARA-EXIT. EXIT.
* -----------
* Close files
* -----------
CLOSE-FILES-PARA.
CLOSE ACCT-FILE.
IF WS-ACCT-STATUS NOT = "00"
DISPLAY 'ERROR IN CLOSING FILE :ACCT-FILE'
WS-ACCT-STATUS
END-IF.
CLOSE-FILES-PARA-EXIT. EXIT.
Screenshot while debugging:
Display Module Source
Program: DELETEOP01 Library: TESTLAB Module: DELETEOP01
57 * ------------------------------
58 READ-ACCT-PARA.
59 DELETE ACCT-FILE RECORD
60 IF WS-ACCT-STATUS NOT = 0
61 DISPLAY 'ERROR IN DELETE OPERATION: ' WS-ACCT-STATUS
62 END-IF
63 READ-ACCT-PARA-EXIT. EXIT.
64
65 * -----------
66 * Close files
67 * -----------
68 CLOSE-FILES-PARA.
69 CLOSE ACCT-FILE.
70 IF WS-ACCT-STATUS NOT = "00"
71 DISPLAY 'ERROR IN CLOSING FILE :ACCT-FILE'
More...
Debug . . .
F3=End program F6=Add/Clear breakpoint F10=Step F11=Display variable
F12=Resume F17=Watch variable F18=Work with watch F24=More keys
WS-ACCT-STATUS = '94'
Output:
ERROR IN DELETE OPERATION: 94
Example 2: DELETE verb status handling (When Status code = ‘9S’)
IDENTIFICATION DIVISION.
*------------------------*
PROGRAM-ID. DELETEOP02.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
*---------------------*
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBMi-Series.
OBJECT-COMPUTER. IBMi-Series.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT ACCT-FILE
ASSIGN TO DATABASE-ACCTPF
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS EXTERNALLY-DESCRIBED-KEY
WITH DUPLICATES
FILE STATUS IS WS-ACCT-STATUS.
DATA DIVISION.
*--------------*
FILE SECTION.
FD ACCT-FILE
LABEL RECORDS ARE STANDARD.
01 ACCT-RCD.
COPY DDS-ALL-FORMATS OF ACCTPF
WITH PREFIX BY 'A-'.
WORKING-STORAGE SECTION.
*-----------------------*
77 WS-ACCT-STATUS PIC XX.
PROCEDURE DIVISION.
*-------------------*
MAINLINE.
PERFORM OPEN-FILES-PARA
THRU OPEN-FILES-PARA-EXIT.
PERFORM READ-ACCT-PARA
THRU READ-ACCT-PARA-EXIT.
PERFORM CLOSE-FILES-PARA
THRU CLOSE-FILES-PARA-EXIT.
STOP RUN.
* ----------
* Open Files
* ----------
OPEN-FILES-PARA.
OPEN I-O ACCT-FILE.
IF WS-ACCT-STATUS NOT = '00'
DISPLAY 'ERROR IN OPENING FILE: ACCT-FILE' WS-ACCT-STATUS
END-IF.
OPEN-FILES-PARA-EXIT. EXIT.
* ------------------------------
* Read sequentially with no lock
* ------------------------------
READ-ACCT-PARA.
READ ACCT-FILE NEXT RECORD WITH NO LOCK
IF WS-ACCT-STATUS NOT = 0
DELETE ACCT-FILE RECORD
IF WS-ACCT-STATUS NOT = 0
DISPLAY 'ERROR IN DELETE OPERATION: ' WS-ACCT-STATUS
END-IF
END-IF.
READ-ACCT-PARA-EXIT. EXIT.
* -----------
* Close files
* -----------
CLOSE-FILES-PARA.
CLOSE ACCT-FILE.
IF WS-ACCT-STATUS NOT = "00"
DISPLAY 'ERROR IN CLOSING FILE :ACCT-FILE'
WS-ACCT-STATUS
END-IF.
CLOSE-FILES-PARA-EXIT. EXIT.
Screenshot while debugging
Display Module Source
Program: DELETEOP02 Library: TESTLAB Module: DELETEOP02
58 * ------------------------------
59 * Read sequentially with no lock
60 * ------------------------------
61 READ-ACCT-PARA.
62 READ ACCT-FILE NEXT RECORD WITH NO LOCK
63 IF WS-ACCT-STATUS NOT = 0
64 DELETE ACCT-FILE RECORD
65 IF WS-ACCT-STATUS NOT = 0
66 DISPLAY 'ERROR IN DELETE OPERATION: ' WS-ACCT-STATUS
67 END-IF
68 END-IF.
69
70 READ-ACCT-PARA-EXIT. EXIT.
71
72 * -----------
More...
Debug . . .
F3=End program F6=Add/Clear breakpoint F10=Step F11=Display variable
F12=Resume F17=Watch variable F18=Work with watch F24=More keys
WS-ACCT-STATUS = '9S'
Output:
ERROR IN DELETE OPERATION: 9S
7. START
►For index and relative file with unique or non-unique constraint, if status code != 0 then start file operation is failed.
Example 2: DELETE verb status handling (When Status code = ‘9S’)
IDENTIFICATION DIVISION.
*------------------------*
PROGRAM-ID. DELETEOP03.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
*---------------------*
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBMi-Series.
OBJECT-COMPUTER. IBMi-Series.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT ACCT-FILE
ASSIGN TO DATABASE-ACCTPF
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS EXTERNALLY-DESCRIBED-KEY
WITH DUPLICATES
FILE STATUS IS WS-ACCT-STATUS.
DATA DIVISION.
*--------------*
FILE SECTION.
FD ACCT-FILE
LABEL RECORDS ARE STANDARD.
01 ACCT-RCD.
COPY DDS-ALL-FORMATS OF ACCTPF
WITH PREFIX BY 'A1-’.
WORKING-STORAGE SECTION.
*-----------------------*
77 WS-ACCT-STATUS PIC XX.
PROCEDURE DIVISION.
*-------------------*
MAINLINE.
PERFORM OPEN-FILES-PARA
THRU OPEN-FILES-PARA-EXIT.
PERFORM READ-ACCT-PARA
THRU READ-ACCT-PARA-EXIT.
PERFORM CLOSE-FILES-PARA
THRU CLOSE-FILES-PARA-EXIT.
STOP RUN.
* ----------
* Open Files
* ----------
OPEN-FILES-PARA.
OPEN I-O ACCT-FILE.
IF WS-ACCT-STATUS NOT = '00'
DISPLAY 'ERROR IN OPENING FILE: ACCT-FILE' WS-ACCT-STATUS
END-IF.
OPEN-FILES-PARA-EXIT. EXIT.
* ------------------------------
* Read sequentially with no lock
* ------------------------------
READ-ACCT-PARA.
READ ACCT-FILE NEXT RECORD WITH NO LOCK
IF WS-ACCT-STATUS NOT = 0
DELETE ACCT-FILE RECORD
IF WS-ACCT-STATUS NOT = 0
DISPLAY 'ERROR IN DELETE OPERATION: ' WS-ACCT-STATUS
END-IF
END-IF.
READ-ACCT-PARA-EXIT. EXIT.
* -----------
* Close files
* -----------
CLOSE-FILES-PARA.
CLOSE ACCT-FILE.
IF WS-ACCT-STATUS NOT = "00"
DISPLAY 'ERROR IN CLOSING FILE :ACCT-FILE'
WS-ACCT-STATUS
END-IF.
CLOSE-FILES-PARA-EXIT. EXIT.
Screenshot while debugging
Display Module Source
Program: DELETEOP02 Library: TESTLAB Module: DELETEOP02
58 * ------------------------------
59 * Read sequentially with no lock
60 * ------------------------------
61 READ-ACCT-PARA.
62 READ ACCT-FILE NEXT RECORD WITH NO LOCK
63 IF WS-ACCT-STATUS NOT = 0
64 DELETE ACCT-FILE RECORD
65
66 DISPLAY 'ERROR IN DELETE OPERATION: ' WS-ACCT-STATUS
67 END-IF
68 END-IF.
69
70 READ-ACCT-PARA-EXIT. EXIT.
71
72 * -----------
More...
Debug . . .
F3=End program F6=Add/Clear breakpoint F10=Step F11=Display variable
F12=Resume F17=Watch variable F18=Work with watch F24=More keys
WS-ACCT-STATUS = '9S'
Output:
ERROR IN DELETE OPERATION: 9S
Decimal Data Errors
MCH1202 – Decimal Data Error.
A Decimal Data Error in ILE COBOL (system message MCH1202) occurs when a numeric field contains non-numeric data or is improperly initialized. This error cannot be trapped by ON SIZE ERROR phrase or via PROGRAM STATUS clause. There are multiple reasons which can result to MCH1202. Listed few of them:
- Uninitialized numeric subfields of a group variable containing blanks (x’40’) trigger a decimal data error when used with DISPLAY, MOVE or arithmetic operations.
- Moving null values (x’00’) from null-capable fields into non-null-capable numeric fields via the MOVE statement results in a Decimal Data Error.
- If a numeric field within a file contains a junk or garbage value (+++), the READ operation completes successfully; however, the system crashes during DISPLAY, MOVE or arithmetic operations due to a decimal data error.
- If an alphanumeric field is moved to numeric field, it can trigger a decimal data error.
Case#01 – Uninitialized numeric subfields of a group variable containing blanks (x’40’) trigger a decimal data error when used with DISPLAY, MOVE or arithmetic operations.
IDENTIFICATION DIVISION.
*------------------------*
PROGRAM-ID. DECMALDATA.
AUTHOR. PROGRAMMER.
DATA DIVISION.
*--------------*
01 WS-GROUP-VARIABLE.
05 WS-NUM1 PIC 9(5)V9(2) COMP-3.
05 WS-NUM2 PIC 9(5)V9(2) COMP-3.
05 WS-NUM3 PIC 9(5)V9(2) COMP-3.
PROCEDURE DIVISION.
*-------------------*
MAINLINE.
MOVE WS-NUM1 TO WS-NUM2.
STOP RUN.
Runtime error:
Message . . . . : Message 'MCH1202' in program object 'DECMALDATA' in library 'TESTLAB' (C D F G). Cause . . . . . : Message 'MCH1202' was detected in COBOL statement 20 of COBOL program 'DECMALDATA' in program object 'DECMALDATA' in library 'TESTLAB'.
Precaution to avoid error: Programmers must initialize numeric subfields using VALUE clause in the DATA DIVISION or by initializing the group variable in the PROCEDURE DIVISION.
e.g.:
DATA DIVISION.
*--------------*
01 WS-GROUP-VARIABLE.
05 WS-NUM1 PIC 9(5)V9(2) COMP-3 VALUE 0.
05 WS-NUM2 PIC 9(5)V9(2) COMP-3 VALUE 0.
05 WS-NUM3 PIC 9(5)V9(2) COMP-3 VALUE 0.
e.g.:
PROCEDURE DIVISION.
MAINLINE.
INITIALIZE WS-GROUP-VARIABLE.
MOVE WS-NUM1 TO WS-NUM2.
STOP RUN.
Case#02 – Moving null values (x’00’) from null-capable fields into non-null-capable numeric fields via the MOVE statement results in a Decimal Data Error.
IDENTIFICATION DIVISION.
*-----------------------*
PROGRAM-ID. DECMALDAT2.
AUTHOR. PROGRAMMER.
DATA DIVISION.
*--------------*
01 DB-FIELDS.
05 DB-PRICE PIC S9(7)V9(2) COMP-3.
05 DB-PRICE-IND PIC S9(4) COMP-5.
01 DB-NUM4 PIC 9(5)V9(2) COMP-3.
01 WS-PSDS.
05 FILLER PIC X(46).
05 WS-EXCP-ID PIC X(07).
PROCEDURE DIVISION.
*-------------------*
MAINLINE.
MOVE -1 TO DB-PRICE-IND
MOVE DB-PRICE TO DB-NUM4.
STOP RUN.
Runtime error:
Message . . . . : Message 'MCH1202' in program object 'DECMALDAT2' in library 'TESTLAB' (C D F G). Cause . . . . . : Message 'MCH1202' was detected in COBOL statement 21 of COBOL program 'DECMALDAT2' in program object 'DECMALDAT2' in library 'TESTLAB'.
Precaution to avoid error: Programmers must check if the null indicator is -1 before performing a MOVE operation on the data field.
e.g.:
PROCEDURE DIVISION.
*-------------------*
MAINLINE.
MOVE -1 TO DB-PRICE-IND.
IF DB-PRICE-IND = -1
MOVE 0 TO DB-NUM4
ELSE
MOVE DB-PRICE TO DB-NUM4
END-IF.
STOP RUN.
Case#03 – If a numeric field within a file contains a junk or garbage value (+++), the READ operation completes successfully; however, the system crashes during DISPLAY, MOVE or arithmetic operations due to a decimal data error.
IDENTIFICATION DIVISION.
*------------------------*
PROGRAM-ID. READ01.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
*---------------------*
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBMi-Series.
OBJECT-COMPUTER. IBMi-Series.
INPUT-OUTPUT SECTION.
*---------------------*
FILE-CONTROL.
SELECT ACCT-FILE
ASSIGN TO DATABASE-ACCOUNT
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS EXTERNALLY-DESCRIBED-KEY
FILE STATUS IS WS-ACCT-STATUS.
SELECT ACCTX-FILE
ASSIGN TO DATABASE-ACCOUNTX
ORGANIZATION IS INDEXED
ACCESS MODE IS SEQUENTIAL
RECORD KEY IS EXTERNALLY-DESCRIBED-KEY
FILE STATUS IS WS-ACCTX-STATUS.
DATA DIVISION.
*--------------*
FILE SECTION.
FD ACCT-FILE
LABEL RECORDS ARE STANDARD.
01 ACCT-RCD.
COPY DDS-ALL-FORMATS OF ACCOUNT.
FD ACCTX-FILE
LABEL RECORDS ARE STANDARD.
01 ACCTX-RCD.
COPY DDS-ALL-FORMATS OF ACCOUNTX.
WORKING-STORAGE SECTION.
*-----------------------*
77 WS-ACCT-STATUS PIC XX.
77 WS-ACCTX-STATUS PIC XX.
*
*------------------*
PROCEDURE DIVISION.
*------------------*
MAINLINE.
PERFORM OPEN-FILES-PARA
THRU OPEN-FILES-PARA-EXIT.
PERFORM READ-ACCT-PARA
THRU READ-ACCT-PARA-EXIT.
PERFORM CLOSE-FILES-PARA
THRU CLOSE-FILES-PARA-EXIT.
STOP RUN.
* ----------
* Open Files
* ----------
OPEN-FILES-PARA.
OPEN INPUT ACCT-FILE
OUTPUT ACCTX-FILE.
IF WS-ACCT-STATUS NOT = '00'
DISPLAY 'ERROR IN OPENING FILE: ACCT-FILE'
WS-ACCT-STATUS
END-IF.
IF WS-ACCTX-STATUS NOT = '00'
DISPLAY 'ERROR IN OPENING FILE: ACCTX-FILE'
WS-ACCTX-STATUS
END-IF.
OPEN-FILES-PARA-EXIT. EXIT.
* -----------------------------
* Read all records sequentially
* -----------------------------
READ-ACCT-PARA.
READ ACCT-FILE FIRST RECORD
IF WS-ACCT-STATUS = "10"
GO TO READ-ACCT-PARA-EXIT
ELSE
IF WS-ACCT-STATUS NOT = "00"
DISPLAY 'ERROR IN READING FILE: ACCT-FILE
WS-ACCT-STATUS
END-IF
END-IF.
READ ACCT-FILE PRIOR RECORD
IF WS-ACCT-STATUS = "10"
GO TO READ-ACCT-PARA-EXIT
ELSE
IF WS-ACCT-STATUS NOT = "00"
DISPLAY 'ERROR IN READING FILE: ACCT-FILE'
WS-ACCT-STATUS
END-IF
END-IF.
INITIALIZE ACCTX-RCD.
MOVE DIVID OF ACCT-FILE TO DIVID OF ACCTX-FILE.
MOVE ACCTNO OF ACCT-FILE TO ACCTNO OF ACCTX-FILE.
MOVE NAME OF ACCT-FILE TO NAME OF ACCTX-FILE.
MOVE DEPT OF ACCT-FILE TO DEPT OF ACCTX-FILE.
MOVE ADDR OF ACCT-FILE TO ADDR OF ACCTX-FILE.
MOVE CITY OF ACCT-FILE TO CITY OF ACCTX-FILE.
MOVE STATE OF ACCT-FILE TO STATE OF ACCTX-FILE.
MOVE ZIP OF ACCT-FILE TO ZIP OF ACCTX-FILE.
MOVE PHONE OF ACCT-FILE TO PHONE OF ACCTX-FILE.
MOVE LASTACTD OF ACCT-FILE TO LASTACT OF ACCTX-FILE.
READ-ACCT-PARA-EXIT. EXIT.
* -----------
* Close files
* -----------
CLOSE-FILES-PARA.
CLOSE ACCT-FILE
ACCTX-FILE.
IF WS-ACCT-STATUS NOT = "00"
DISPLAY 'ERROR IN CLOSING FILE :ACCT-FILE'
WS-ACCT-STATUS
END-IF.
IF WS-ACCTX-STATUS NOT = "00"
DISPLAY 'ERROR IN CLOSING FILE :ACCTX-FILE'
WS-ACCTX-STATUS
END-IF.
CLOSE-FILES-PARA-EXIT. EXIT.
Runtime error:
Message . . . . : Message 'MCH1202' in program object 'READ01' in library 'TESTLAB' (C D F G). Cause . . . . . : Message 'MCH1202' was detected in COBOL statement 138 of COBOL program 'READ01' in program object 'READ01' in library 'TESTLAB'.
Precaution to avoid error: Programmers must check whether numeric fields have junk data(+++) and code accordingly.
e.g.:
IF LASTACTD OF ACCT-FILE IS NUMERIC
MOVE LASTACTD OF ACCT-FILE
TO LASTACT OF ACCTX-FILE
ELSE
MOVE 0 TO LASTACT OF ACCTX-FILE
END-IF.
Case#04 – If an alphanumeric field is moved to numeric field, it can trigger a decimal data error.
IDENTIFICATION DIVISION.
*------------------------*
PROGRAM-ID. DECMALDAT4.
AUTHOR. PROGRAMMER.
DATA DIVISION.
*--------------*
01 WS-CHAR1 PIC X(10) VALUE ’10#1‘.
01 WS-NUM1 PIC 9(10).
PROCEDURE DIVISION.
*-------------------*
MAINLINE.
MOVE WS-CHAR1 TO WS-NUM1.
STOP RUN.
Runtime error:
Message . . . . : Message 'MCH1202' in program object 'DECMALDAT4' in library 'TESTLAB' (C D F G). Cause . . . . . : Message 'MCH1202' was detected in COBOL statement 20 of COBOL program 'DECMALDAT4' in program object 'DECMALDAT4' in library 'TESTLAB'.
Precaution to avoid error: Programmers must check whether alphanumeric fields have numeric data only and code accordingly. In current scenario, extra ‘#’ is giving decimal data error.
e.g.:
PROCEDURE DIVISION.
MAINLINE.
IF WS-CHAR1 IS NUMERIC
MOVE WS-CHAR1 TO WS-NUM1
ELSE
MOVE 0 TO WS-NUM1
END-IF.
STOP RUN.
Arithmetic Operations Error Handling
The arithmetic operations (e.g. ADD, SUBTRACT, MULTIPLY, DIVIDE, or COMPUTE) will either produce a larger value than the target variable size or have tried dividing by zero at runtime. The program execution fails because of the above error. ON SIZE ERROR phrase can handle the scenarios if they are coded with arithmetic statements.
Example 1: Handling SIZE ERROR on COMPUTE verb
IDENTIFICATION DIVISION. * -----------------------* PROGRAM-ID. ERRHNDL01. AUTHOR. PROGRAMMER. DATA DIVISION. * ------------* WORKING-STORAGE SECTION. 01 WS-NUM-VAR PIC 9(03). 01 WS-RES-VAR PIC 9(03). PROCEDURE DIVISION. * -----------------------* COMPUTE WS-NUM-VAR = 100. COMPUTE WS-RES-VAR = WS-NUM-VAR * 10 ON SIZE ERROR DISPLAY 'ERROR: SIZE-ERROR ON COMPUTE' END-COMPUTE. DISPLAY 'WS-RES-VAR:' WS-RES-VAR. STOP RUN.
Output:
ERROR: SIZE-ERROR ON COMPUTE WS-RES-VAR:000
Example 2: Handling DIVIDE BY ZERO on COMPUTE verb
IDENTIFICATION DIVISION. * -----------------------* PROGRAM-ID. ERRHNDL02. AUTHOR. PROGRAMMER. DATA DIVISION. * ------------* WORKING-STORAGE SECTION. 01 WS-NUM-VAR PIC 9(02). 01 WS-RES-VAR PIC 9(03) VALUE 10. PROCEDURE DIVISION. * ------------------* COMPUTE WS-NUM-VAR = 100. COMPUTE WS-RES-VAR = 100/WS-NUM-VAR ON SIZE ERROR DISPLAY 'ERROR: DIVIDE BY ZERO CASE'. DISPLAY 'WS-NUM-VAR:' WS-NUM-VAR. DISPLAY 'WS-RES-VAR:' WS-RES-VAR. STOP RUN.
Output:
ERROR: DIVIDE BY ZERO CASE WS-NUM-VAR:00 WS-RES-VAR:010
Example 3: Handling ON SIZE ERROR on COMPUTE & ADD verb
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. ERRHNDL03.
AUTHOR. PROGRAMMER.
DATA DIVISION.
* -------------*
WORKING-STORAGE SECTION.
01 GST-VARIABLES.
05 WS-BASE-AMOUNT PIC 9(4)V9(2) VALUE 1499.99.
05 WS-GST-SPLIT-RATE PIC V99 VALUE .09.
05 WS-CGST-AMOUNT PIC 9(2)V9(2).
05 WS-SGST-AMOUNT PIC 9(2)V9(2).
05 WS-FINAL-AMOUNT PIC 9(4)V9(2).
05 WS-MESSAGE PIC X(70).
PROCEDURE DIVISION.
* ------------------*
MAINLINE-PROGRAMMING.
PERFORM CALCULATE-AFTERTAX-PARA
THRU CALCULATE-AFTERTAX-PARA-EXIT.
DISPLAY 'WS-FINAL-AMOUNT:' WS-FINAL-AMOUNT.
STOP RUN.
CALCULATE-AFTERTAX-PARA.
* ------------------*
* Calculate SGST Amount
COMPUTE WS-SGST-AMOUNT ROUNDED
= WS-BASE-AMOUNT
* WS-GST-SPLIT-RATE
ON SIZE ERROR
MOVE 'ERROR: In calculation of SGST-AMOUNT'
TO WS-MESSAGE
PERFORM HANDLE-ERROR-PARA
THRU HANDLE-ERROR-PARA-EXIT
END-COMPUTE.
* Calculate CGST Amount
COMPUTE WS-CGST-AMOUNT ROUNDED
= WS-BASE-AMOUNT
* WS-GST-SPLIT-RATE
ON SIZE ERROR
MOVE 'ERROR: In calculation of CGST-AMOUNT'
TO WS-MESSAGE
PERFORM HANDLE-ERROR-PARA
THRU HANDLE-ERROR-PARA-EXIT
END-COMPUTE.
* Calculate Final Amount
COMPUTE WS-FINAL-AMOUNT ROUNDED
= WS-BASE-AMOUNT
+ WS-CGST-AMOUNT
+ WS-SGST-AMOUNT
ON SIZE ERROR
MOVE 'ERROR: In calculation of FINAL-AMOUNT'
TO WS-MESSAGE
PERFORM HANDLE-ERROR-PARA
THRU HANDLE-ERROR-PARA-EXIT
END-COMPUTE.
CALCULATE-AFTERTAX-PARA-EXIT. EXIT.
* Handle & show error
HANDLE-ERROR-PARA.
DISPLAY WS-MESSAGE.
STOP RUN.
HANDLE-ERROR-PARA-EXIT. EXIT.
Output:
ERROR: In calculation of SGST-AMOUNT
Error Handling via phrases
COBOL does not automatically take corrective action when file input or output operation fails. The user should choose whether the program will continue or fail when an I-O error occurs. The techniques below are used for handling specific input or output conditions or errors –
- AT END and NOT AT END – Handling end of file or beginning of file condition for COBOL verbs.
1. READ filename NEXT RECORD AT END
2. READ filename PRIOR RECORD AT END
3. READ filename FIRST RECORD AT END
4. READ filename LAST RECORD AT END
5. RETURN filename AT END
6. SEARCH identifier AT END
7. WRITE filename(printer) AT EOP
- INVALID KEY and NOT INVALID KEY – Handling record not found for COBOL verbs.
1. DELETE filename INVALID KEY
2. READ filename RECORD INVALID KEY
3. REWRITE filename INVALID KEY
4. START filename KEY >= EXTERNALLY DESCRIBED KEY INVALID KEY
5. WRITE filename INVALID KEY
- NO LOCK – Handling locks for COBOL verbs.
1. DELETE filename and REWRITE filename fails as last READ operation specified NO LOCK
2. READ filename NO LOCK
3. START filename NO LOCK
String Operations Error Handling
During the STRING and UNSTRING verbs operations the result string length may fall outside the size of the receiving variable. This causes string truncation and does not notify the user. The ON OVERFLOW phase is used to handle the overflow condition and notify the end user about it.
Example 1: ON OVERFLOW in STRING
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. ERRHNDL11.
AUTHOR. PROGRAMMER.
DATA DIVISION.
* ------------*
WORKING-STORAGE SECTION.
01 WS-PART1 PIC X(05) VALUE 'HELLO'.
01 WS-PART2 PIC X(05) VALUE 'WORLD'.
01 WS-STRING PIC X(08).
PROCEDURE DIVISION.
* ------------------*
STRING WS-PART1 DELIMITED BY SIZE
WS-PART2 DELIMITED BY SIZE
INTO WS-STRING
ON OVERFLOW
DISPLAY 'ERROR: STRING OVERFLOW OCCURED!'
END-STRING.
DISPLAY 'WS-STRING: ' WS-STRING.
STOP RUN.
Output:
ERROR: STRING OVERFLOW OCCURED! WS-STRING: HELLOWOR
Note: Error occurred WS-LOCATION-BIN is truncated but OVERFLOW clause does not work properly.
Example 2: ON OVERFLOW in UNSTRING
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. ERRHNDL12.
AUTHOR. PROGRAMMER.
DATA DIVISION.
* ------------*
WORKING-STORAGE SECTION.
01 WS-ORDER-STATUS-DETAIL PIC X(100).
01 WS-ORDER-ID PIC X(07).
01 WS-ORDER-LIN-NBR PIC 9(02).
01 WS-PRODUCT-ID PIC X(08).
01 WS-QUANTITY-ORDERED PIC X(08).
01 WS-LOCATION-BIN PIC X(10).
01 WS-PICKING-STATUS PIC X(10).
01 WS-SHIPPED-DATE PIC X(10).
PROCEDURE DIVISION.
* ------------------*
MOVE 'ORD1001;2;PD-7818;5;W1ZBA15R02L04P;Pending;'
TO WS-ORDER-STATUS-DETAIL
UNSTRING WS-ORDER-STATUS-DETAIL
DELIMITED BY ';'
INTO WS-ORDER-ID,
WS-ORDER-LIN-NBR,
WS-ORDER-LIN-NBR,
WS-PRODUCT-ID,
WS-QUANTITY-ORDERED,
WS-LOCATION-BIN,
WS-PICKING-STATUS,
WS-SHIPPED-DATE
ON OVERFLOW
DISPLAY 'WARNING: UNSTRING OPERATION FAILED!'
NOT ON OVERFLOW
DISPLAY 'SUCCESSFUL!'
END-UNSTRING.
DISPLAY 'WS-ORDER-ID :' WS-ORDER-ID.
DISPLAY 'WS-ORDER-LIN-NBR :' WS-ORDER-LIN-NBR.
DISPLAY 'WS-PRODUCT-ID :' WS-PRODUCT-ID.
DISPLAY 'WS-QUANTITY-ORDERED:' WS-QUANTITY-ORDERED.
DISPLAY 'WS-LOCATION-BIN :' WS-LOCATION-BIN.
DISPLAY 'WS-PICKING-STATUS :' WS-PICKING-STATUS.
DISPLAY 'WS-SHIPPED-DATE :' WS-SHIPPED-DATE.
STOP RUN.
Output:
SUCCESSFUL! WS-ORDER-ID :ORD1001 WS-ORDER-LIN-NBR :02 WS-PRODUCT-ID :PD-7818 WS-QUANTITY-ORDERED:5 WS-LOCATION-BIN :W1ZBA15R02 WS-PICKING-STATUS :Pending WS-SHIPPED-DATE :
Note: WS-LOCATION-BIN is truncated but OVERFLOW clause does not work correctly in UNSTRING verb.
Example 3: ON OVERFLOW in UNSTRING
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. ERRHNDL12.
AUTHOR. PROGRAMMER.
DATA DIVISION.
* ------------*
WORKING-STORAGE SECTION.
01 WS-ORDER-STATUS-DETAIL PIC X(100).
01 WS-ORDER-ID PIC X(07).
01 WS-ORDER-LIN-NBR PIC 9(02).
01 WS-PRODUCT-ID PIC X(08).
01 WS-QUANTITY-ORDERED PIC X(08).
01 WS-LOCATION-BIN PIC X(10).
01 WS-PICKING-STATUS PIC X(10).
01 WS-SHIPPED-DATE PIC X(10).
PROCEDURE DIVISION.
* ------------------*
MOVE 'ORD1001;2;PD-7818;5;W1ZBA15R02;Pending;06-06-2025;'
TO WS-ORDER-STATUS-DETAIL
UNSTRING WS-ORDER-STATUS-DETAIL
DELIMITED BY ';'
INTO WS-ORDER-ID,
WS-ORDER-LIN-NBR,
WS-ORDER-LIN-NBR,
WS-PRODUCT-ID,
WS-QUANTITY-ORDERED,
WS-LOCATION-BIN,
WS-PICKING-STATUS,
WS-SHIPPED-DATE
ON OVERFLOW
DISPLAY 'WARNING: UNSTRING OPERATION FAILED!'
NOT ON OVERFLOW
DISPLAY 'SUCCESSFUL!'
END-UNSTRING.
DISPLAY 'WS-ORDER-ID :' WS-ORDER-ID.
DISPLAY 'WS-ORDER-LIN-NBR :' WS-ORDER-LIN-NBR.
DISPLAY 'WS-PRODUCT-ID :' WS-PRODUCT-ID.
DISPLAY 'WS-QUANTITY-ORDERED:' WS-QUANTITY-ORDERED.
DISPLAY 'WS-LOCATION-BIN :' WS-LOCATION-BIN.
DISPLAY 'WS-PICKING-STATUS :' WS-PICKING-STATUS.
DISPLAY 'WS-SHIPPED-DATE :' WS-SHIPPED-DATE.
STOP RUN.
Output:
WARNING: UNSTRING OPERATION FAILED! WS-ORDER-ID :ORD1001 WS-ORDER-LIN-NBR :02 WS-PRODUCT-ID :PD-7818 WS-QUANTITY-ORDERED:5 WS-LOCATION-BIN :W1ZBA15R02 WS-PICKING-STATUS :Pending WS-SHIPPED-DATE :06-06-2025
Note: UNSTRING operation failed as string as extra semi-colon.
CALL Operations Error Handling
In ILE COBOL programs, programmers use CALL statements for:
- Calling a subprogram – (Object Type – *PGM, Attribute – CBL or CBLLE)
- Calling an external API programs like QCMDEXC. (Object Type – *PGM, Attribute – CBL)
- Calling internal procedures in NOMONOPRC.
- Calling statis or dynamic call.
ON EXCEPTION phrase handles few errors at run time. ON OVERFLOW phrase with CALL verb works similar as ‘EXCEPTION’.
Example 1: CALL (Handling error MCH3401 – Cannot resolve object ‘ABC’ & ‘XYZ’.)
PROCESS APOST.
IDENTIFICATION DIVISION.
*-----------------------*
PROGRAM-ID. ERRHNDL31.
AUTHOR. PROGRAMMER.
DATA DIVISION.
*--------------*
01 WS-PGM-NAME PIC X(10) VALUE 'XYZ'.
PROCEDURE DIVISION.
*-------------------*
MAIN-LOGIC.
* Call 'ABC' program
CALL 'ABC'
ON EXCEPTION
DISPLAY 'ERROR IN CALL - ABC'.
* Call 'XYZ' program
CALL WS-PGM-NAME
ON EXCEPTION
DISPLAY 'ERROR IN CALL - XYZ'.
STOP RUN.
Output:
ERROR IN CALL – ABC ERROR IN CALL – XYZ
►Since, ABC & XYZ – *PGM object was not present in *LIBL.
►This works well for – object types – *PGM, Attribute – CBL, CBLLE, CLP or CLLE.
Example 2: Handling error MCH0801 – Argument associated with external or internal parameters not passed.
PROCESS APOST. IDENTIFICATION DIVISION. *-----------------------* PROGRAM-ID. ERRHNDL32. AUTHOR. PROGRAMMER. DATA DIVISION. *--------------* 01 WS-PGM-NAME PIC X(10) VALUE 'ABC02'. PROCEDURE DIVISION. *-------------------* MAIN-LOGIC. * Call program 'ABC02' - MCH0801 * ------------------------------ CALL 'ABC02' ON EXCEPTION DISPLAY 'ERROR IN CALL - ABC02'. STOP RUN.
Program: ABC02
IDENTIFICATION DIVISION. *-----------------------* PROGRAM-ID. ABC02. AUTHOR. PROGRAMMER. DATA DIVISION. *--------------* LINKAGE SECTION. 01 LK-PARM1 PIC X(02). PROCEDURE DIVISION USING LK-PARM1. *-------------------* MAIN-LOGIC. DISPLAY "LK-PARM1:" LK-PARM1. STOP RUN.
Output
ERROR IN CALL - ABC02.
►Since, no parameter has been passed for ABC02.
► This works well for – object types – *PGM, Attribute – CBL, CBLLE, CLP or CLLE.
Example 3: CALL (Handling error MCH0802 – Total parameters passed do not match the number required.)
PROCESS APOST.
IDENTIFICATION DIVISION.
*-----------------------*
PROGRAM-ID. ERRHNDL33.
AUTHOR. PROGRAMMER.
DATA DIVISION.
*--------------*
01 WS-PARM1 PIC X(05) VALUE 'QWERT'.
01 WS-PARM2 PIC X(05) VALUE 'OPERT'.
PROCEDURE DIVISION.
*-------------------*
MAIN-LOGIC.
* Call program 'ABC01' - MCH0802
* ------------------------------
CALL 'ABC01' USING WS-PARM1
ON EXCEPTION
DISPLAY 'ERROR IN CALL - ABC01'.
STOP RUN.
Program: ABC01
IDENTIFICATION DIVISION.
*-----------------------*
PROGRAM-ID. ABC01.
AUTHOR. PROGRAMMER.
DATA DIVISION.
*--------------*
LINKAGE SECTION.
01 LK-PARM1 PIC X(05).
01 LK-PARM2 PIC X(05).
PROCEDURE DIVISION.
*-------------------*
MAIN-LOGIC.
DISPLAY "LK-PARM1:" LK-PARM1.
DISPLAY "LK-PARM2:" LK-PARM2.
STOP RUN.
Output
ERROR IN CALL - ABC01.
► There is a mismatch in number of parameters when ABC02 program is called.
► This works well for – object types – *PGM, attribute – CBL or CBLLE.
► This does not work well for – object types – *PGM, attribute – CL or CLLE.
Divide by Zero Errors
MCH1211 – Divide by Zero Error.
The MCH1211 message in ILE COBOL indicates an attempt to divide by zero during a fixed-point arithmetic operation. This error occurs when the divisor in a DIVIDE or COMPUTE statement has a value of zero. This is a common runtime error that can cause a program to terminate if not handled.
Example#01 – DIVIDE BY ZERO error.
IDENTIFICATION DIVISION.
*------------------------*
PROGRAM-ID. DIVIDEBY0.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
*---------------------*
CONFIGURATION SECTION.
SPECIAL-NAMES. PROGRAM STATUS IS WS-PSDS.
DATA DIVISION.
*--------------*
01 WS-NUM1 PIC 9(5)V9(2) COMP-3 VALUE 100.05.
01 WS-NUM2 PIC 9(5)V9(2) COMP-3 VALUE 0.00.
01 WS-NUM3 PIC 9(5)V9(2) COMP-3 VALUE 0.12.
01 WS-PSDS.
05 FILLER PIC X(46).
05 WS-EXCP-ID PIC X(07).
05 FILLER PIC X(41).
PROCEDURE DIVISION.
*------------------------*
MAINLINE.
COMPUTE WS-NUM3 = WS-NUM1/WS-NUM2
ON SIZE ERROR
IF WS-EXCP-ID = 'MCH1211'
DISPLAY 'ATTEMPT TO DIVIDE BY ZERO'
END-IF.
STOP RUN.
Screenshot during debugging:
Program: DIVIDEBY0 Library: TESTLAB Module: DIVIDEBY0
13 05 WS-EXCP-ID PIC X(07).
14 05 FILLER PIC X(41).
15 PROCEDURE DIVISION.
16 MAINLINE.
17 COMPUTE WS-NUM3 = WS-NUM1/WS-NUM2
18 ON SIZE ERROR
19 IF WS-EXCP-ID = 'MCH1211'
20 DISPLAY 'ATTEMPT TO DIVIDE BY ZERO'
21 END-IF.
22 STOP RUN.
Debug . . .
F3=End program F6=Add/Clear breakpoint F10=Step F11=Display variable
F12=Resume F17=Watch variable F18=Work with watch F24=More keys
WS-EXCP-ID = 'MCH1211'
Output:
ATTEMPT TO DIVIDE BY ZERO
Important points:
- If ‘ON SIZE ERROR’ clause is not used and error occurred, it does not show any error message, and control goes to next executable line.
- Programmers must always use ‘ON SIZE ERROR’ clause whenever DIVIDE verb is used. Also, where divide operation is used in COMPUTE verb for handling divide by zero error.
- Programmers can trap this error only via ‘ON SIZE ERROR’ clause and before end of implicit or explicit COMPUTE/DIVIDE verb.
Display Module Source
Program: DIVIDEBY1 Library: TESTLAB Module: DIVIDEBY1
13 05 WS-EXCP-ID PIC X(07).
14 05 FILLER PIC X(41).
15 PROCEDURE DIVISION.
16 MAINLINE.
17 COMPUTE WS-NUM3 = WS-NUM1/WS-NUM2.
18 IF WS-EXCP-ID = 'MCH1211'
19 DISPLAY 'ATTEMPT TO DIVIDE BY ZERO'
20 END-IF.
21 STOP RUN.
Bottom
Debug . . .
F3=End program F6=Add/Clear breakpoint F10=Step F11=Display variable
F12=Resume F17=Watch variable F18=Work with watch F24=More keys
WS-EXCP-ID = ' '
- In this above code, COMPUTE verb is ended with period (‘.’). When checked value of WS-EXCP-ID field during debug, it was blank, through out the code.
Program Status Clause
In ILE COBOL, the PROGRAM STATUS clause is a powerful diagnostic tool defined in the SPECIAL-NAMES paragraph of the ENVIRONMENT DIVISION. It automatically populates a 94-character group variable with details about runtime exceptions. The first 46 characters contain program information, while positions 47–53 hold the specific exception message ID (like CPF or MCH codes). By evaluating this ID, developers can implement custom error-handling logic to prevent abrupt program termination. Also, develop immediate message that can be provided with necessary impromptu messages in job logs, programs, or workstations. This step always works well when followed by clauses like ON SIZE ERROR, ON OVERFLOW, ON ERROR/EXCEPTION, etc.
Example:
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. ERR3687.
AUTHOR. PROGRAMMERS.
ENVIRONMENT DIVISION.
* --------------------*
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-ISERIES.
OBJECT-COMPUTER. IBM-ISERIES.
SPECIAL-NAMES. PROGRAM STATUS IS WS-PGM-STATUS1.
DATA DIVISION.
* ------------*
WORKING-STORAGE SECTION.
01 WS-RESULT PIC 9(01)V9(02).
01 WS-PGM-STATUS1 PIC X(94).
05 WS-PGM-NAM PIC X(10).
05 WS-PGM-LIB PIC X(10).
05 WS-MOD-NAM PIC X(10).
05 WS-STM-NBR PIC X(10).
05 WS-OPT-LVL PIC X(06).
05 WS-ECP-MSG PIC X(07).
05 WS-JOB-NAM PIC X(10).
05 WS-JOB-NBR PIC X(06).
05 WS-JOB-TYP PIC X(01).
05 WS-USR-PRF PIC X(10).
05 WS-TIM-STAMP PIC X(14).
PROCEDURE DIVISION.
000-MAINLINE-PARA.
* DIVIDE BY ZERO ERROR
DIVIDE 100 BY 0 GIVING WS-RESULT
ON SIZE ERROR PERFORM 011-HANDLE-ERROR-PARA
THRU 011-HANDLE-ERROR-PARA-EXIT
NOT ON SIZE ERROR DISPLAY 'WS-RESULT:' WS-RESULT.
* CALL PROGRAM OBJECT NOT FOUND
CALL "ABC001"
ON EXCEPTION PERFORM 011-HANDLE-ERROR-PARA
THRU 011-HANDLE-ERROR-PARA-EXIT.
STOP RUN.
*
011-HANDLE-ERROR-PARA.
DISPLAY 'WS-PGM-NAM :' WS-PGM-NAM.
DISPLAY 'WS-PGM-LIB :' WS-PGM-LIB.
DISPLAY 'WS-MOD-NAM :' WS-MOD-NAM.
DISPLAY 'WS-STM-NBR :' WS-STM-NBR.
DISPLAY 'WS-OPT-LVL :' WS-OPT-LVL.
DISPLAY 'WS-ECP-MSG :' WS-ECP-MSG.
DISPLAY 'WS-JOB-NAM :' WS-JOB-NAM.
DISPLAY 'WS-JOB-NBR :' WS-JOB-NBR.
DISPLAY 'WS-JOB-TYP :' WS-JOB-TYP.
DISPLAY 'WS-USR-PRF :' WS-USR-PRF.
DISPLAY 'WS-TIM-STAMP:' WS-TIM-STAMP.
011-HANDLE-ERROR-PARA-EXIT.
EXIT.
Output for Divide by zero: (MCH1211)
WS-PGM-NAM :ERR3687 WS-PGM-LIB :PIOLIB WS-MOD-NAM :ERR3687 WS-STM-NBR :26 WS-OPT-LVL :*NONE WS-ECP-MSG :MCH1211 WS-JOB-NAM :QPADEV000K WS-JOB-NBR :847831 WS-JOB-TYP :I WS-USR-PRF :PIOASH WS-TIM-STAMP:20251126012339
Output for Program object not found: (MCH3401)
WS-PGM-NAM :3687 WS-PGM-LIB :PIOLIB WS-MOD-NAM :COBOL12 WS-STM-NBR :29 WS-OPT-LVL :*NONE WS-ECP-MSG :MCH3401 WS-JOB-NAM :QPADEV000K WS-JOB-NBR :847831 WS-JOB-TYP :I WS-USR-PRF :PIOASH WS-TIM-STAMP:20251126012350
Pointer not set error
The monitor message MCH3601 refers to the below three scenarios of errors in ILE COBOL:
TYPE01 : Error occurring due to parameters not passed into called program.
Example – TYPE01 – When no parameter is passed into a called program.
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. ERROR004.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
* --------------------*
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-ISERIES.
OBJECT-COMPUTER. IBM-ISERIES.
DATA DIVISION.
* -------------*
WORKING-STORAGE SECTION.
01 WS-PARM-01 PIC X(05).
LINKAGE SECTION.
01 LK-PARM-01 PIC X(05).
PROCEDURE DIVISION USING LK-PARM-01.
* -----------------------------------*
MAINLINE-PARA.
MOVE LK-PARM-01 TO WS-PARM-01.
STOP RUN.
Screenshot during debugging:
16 PROCEDURE DIVISION USING LK-PARM-01. 17 *█------------------* 18 MAINLINE-PARA. 19 MOVE LK-PARM-01 TO WS-PARM-01. 20 STOP RUN.
Error:
Pointer not set for location referenced.
TYPE02 : Error occurring when a field of the file is used either the file is not opened or closed.
Example – TYPE02 – When a field of a file is used either file is closed or not opened.
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. ERROR005.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
* --------------------*
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-ISERIES.
OBJECT-COMPUTER. IBM-ISERIES.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT ORDER-FILE
ASSIGN TO DATABASE-ORDER
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS EXTERNALLY-DESCRIBED-KEY
DATA DIVISION.
* -------------*
FILE SECTION.
FD ORDER-FILE LABEL RECORDS ARE STANDARD.
01 ORDER-REC.
COPY DDS-ORDERR OF ORDER.
PROCEDURE DIVISION.
* ------------------*
MAINLINE-PARA.
MOVE 20250101 TO ORDERDT.
OPEN INPUT ORDER-FILE.
CLOSE ORDER-FILE.
STOP RUN.
Screenshot during debugging:
22 COPY DDS-ORDERR OF ORDER. 23 PROCEDURE DIVISION. 24 *█------------------* 25 MAINLINE-PARA. 26 MOVE 20250101 TO ORDERDT. 27 OPEN INPUT ORDER-FILE. 28 CLOSE ORDER-FILE. 29 STOP RUN.
Error:
Pointer not set for location referenced.
TYPE03 : Error occurring when a file is not found in the library list and error is not handled.
Example – TYPE03 – When a field of a file is used either file is closed or not opened.
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. ERROR025.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
* --------------------*
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-ISERIES.
OBJECT-COMPUTER. IBM-ISERIES.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT ORDER-FILE
ASSIGN TO DATABASE-ORDER
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS EXTERNALLY-DESCRIBED-KEY
DATA DIVISION.
* -------------*
FILE SECTION.
FD ORDER-FILE LABEL RECORDS ARE STANDARD.
01 ORDER-REC.
COPY DDS-ALL-FORMATS OF ORDER.
PROCEDURE DIVISION.
* ------------------*
MAINLINE-PARA.
OPEN INPUT ORDER-FILE.
MOVE ‘AC100100’ TO ORDERNO.
READ ORDER-FILE.
CLOSE ORDER-FILE.
STOP RUN.
Screenshot during debugging:
Display Module Source
Program: ERRHNDL25 Library: TESTLAB Module: ERRHNDL25
28
29 WORKING-STORAGE SECTION.
30 77 WS-ORDER-STATUS PIC X(02).
31
32 PROCEDURE DIVISION.
33 *-------------------*
34 MAINLINE-PARA.
35 OPEN INPUT ORDER-FILE.
36 IF WS-ORDER-STATUS NOT = '00'
37 DISPLAY WS-ORDER-STATUS
38 END-IF.
39 MOVE 'AC100100' TO ORDERNO OF ORDER-FILE.
40 READ ORDER-FILE RECORD.
41 IF WS-ORDER-STATUS NOT = '00'
42 DISPLAY WS-ORDER-STATUS
More...
Error:
CALL TESTLAB/ERRHNDL25 File ORDER in library *LIBL not found or inline data file missing. 35 Pointer not set for location referenced. Pointer not set for location referenced. Unmonitored exception at line 39
In this scenario, ORDER file is not found (file status = ‘35’), but is not handled, hence, program control reached next sentence and ORDERNO field is not active so ended with error MCH3601.
Range of subscript error
The monitor message MCH0603 refers to the below two types of errors in ILE COBOL: TYPE01: Error occurring due to the invalid range of index values of a table referred to as “Range of subscript error”. It occurs when index-value is either less than the lower bound of 1, or greater than the upper bound defined by the OCCURS clause.
Example – TYPE01 – Range of Subscript errors.
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. ERROR003.
AUTHOR. PROGRAMMER.
DATA DIVISION.
* -------------*
WORKING-STORAGE SECTION.
01 DATA-TABLE.
03 DATA-ITEM PIC X(10) OCCURS 3 TIMES.
01 IDX PIC S9(02).
PROCEDURE DIVISION.
* ------------------*
000-MAINLINE-PARA.
MOVE "DATA" TO DATA-ITEM(IDX).
STOP RUN.
Screenshot for runtime error:
15 01 IDX PIC S9(02). 16 PROCEDURE DIVISION. 17 *█------------------* 18 000-MAINLINE-PARA. 19 MOVE "DATA" TO DATA-ITEM(IDX). 20 STOP RUN.
Error:
Range of subscript value or character string error.
TYPE02: Error occurring due to invalid range of start position or buffer length during reference modification referred to as “Character String Error”. It will occur due to following reasons:
- Case 01: When START-POSITION <1 or START-POSITION > Size of the input String.
- Case 02: When START-POSITION + BUFFER-LENGTH – 1 > Size of the input String.
Example – TYPE 02: Case 01 – Character String Error.
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. ERROR001.
AUTHOR. PROGRAMMER.
DATA DIVISION.
* -------------*
WORKING-STORAGE SECTION.
01 INPUT-STRING-VAR PIC X(34) VALUE 'MCH0603 - RANGE OF SUBSCR
- IPT ERROR'.
01 OUTPUT-STRING-VAR PIC X(10).
01 START-POSITION PIC 9(02) VALUE 21.
01 BUFFER-LENGTH PIC 9(02) VALUE 20.
PROCEDURE DIVISION.
* ------------------*
000-MAINLINE-PARA.
MOVE INPUT-STRING-VAR(START-POSITION:BUFFER-LENGTH)
TO OUTPUT-STRING-VAR.
STOP RUN.
Screenshot for runtime error:
18
19 PROCEDURE DIVISION.
20 *█------------------*
21 000-MAINLINE-PARA.
22 MOVE INPUT-STRING-VAR(START-POSITION:BUFFER-LENGTH)
23 TO OUTPUT-STRING-VAR.
24 STOP RUN.
Error:
Range of subscript value or character string error.
Example – TYPE 02: Case 02 – Character String Error.
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. ERROR002.
AUTHOR. PROGRAMMER.
DATA DIVISION.
* -------------*
WORKING-STORAGE SECTION.
01 INPUT-STRING-VAR PIC X(34) VALUE 'MCH0603 - RANGE OF SUBSCR
- IPT ERROR'.
01 OUTPUT-STRING-VAR PIC X(10).
01 START-POSITION PIC 9(02) VALUE 21.
01 BUFFER-LENGTH PIC 9(02) VALUE 20.
PROCEDURE DIVISION.
* ------------------*
000-MAINLINE-PARA.
MOVE INPUT-STRING-VAR(START-POSITION:BUFFER-LENGTH)
TO OUTPUT-STRING-VAR.
STOP RUN.
Screenshot for runtime error:
18 19 PROCEDURE DIVISION. 20 *█------------------* 21 000-MAINLINE-PARA. 22 MOVE INPUT-STRING-VAR(START-POSITION:BUFFER-LENGTH) 23 TO OUTPUT-STRING-VAR. 24 STOP RUN.
Error:
Range of subscript value or character string error.
Programmers should apply check in the complex programs as below:
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. ERROR001.
AUTHOR. PROGRAMMER.
DATA DIVISION.
* -------------*
WORKING-STORAGE SECTION.
01 INPUT-STRING-VAR PIC X(34) VALUE 'MCH0603 - RANGE OF SUBSCR
- IPT ERROR'.
01 OUTPUT-STRING-VAR PIC X(10).
01 START-POSITION PIC 9(02) VALUE 26.
01 BUFFER-LENGTH PIC 9(02) VALUE 10.
01 ERROR-MESSAGE PIC X(50).
PROCEDURE DIVISION.
* ------------------*
000-MAINLINE-PARA.
IF START-POSITION < 01 OR
START-POSITION > 34 OR
START-POSITION + BUFFER-LENGTH – 1 > 34
MOVE SPACES TO OUTPUT-STRING-VAR
MOVE ‘ERROR!’ TO ERR0R-MESSAGE
ELSE
MOVE INPUT-STRING-VAR(START-POSITION:BUFFER-LENGTH)
TO OUTPUT-STRING-VAR
END-IF.
STOP RUN.