Skip to content

Commit 7c84b1c

Browse files
committed
Add ABEND Handling
Signed-off-by: Hartanto Ario Widjaya <[email protected]>
1 parent ea72535 commit 7c84b1c

File tree

1 file changed

+207
-0
lines changed

1 file changed

+207
-0
lines changed

COBOL Programming Course #1 - Getting Started/COBOL Programming Course #1 - Getting Started.md

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4299,3 +4299,210 @@ Previous lab programs made use of a date/time intrinsic function. The date/time
42994299
**Lab Hints**
43004300

43014301
Refer to CBL0011 line 120 for the proper formatting of the function-name causing the compile error.
4302+
4303+
\newpage
4304+
# ABEND handling
4305+
4306+
When you do the labs on the previous chapters, you may have encountered an abnormal end or ABEND for short. There are various categories of common COBOL errors which cause ABEND, and in production, software errors can be costly - both in financial and reputation.
4307+
4308+
## Why does ABEND happen?
4309+
4310+
Unlike your normal workstation, the mainframe utilizes an instruction set architecture called the z/Architecture. This instruction set describes what instructions can be executed at the lower machine-code level.
4311+
4312+
In the case that the system encounters an instruction that is not permitted under the instruction set, an ABEND will happen. This can happen during compilation, link-edit, or execution of your COBOL program.
4313+
4314+
## Frequent ABEND Types
4315+
4316+
Listed below are nine of the common ABENDs to get you started. Note that there are more ABEND types and situations that you may encounter as a COBOL programmer, and z/OS may sometimes produce a different ABEND code depending on whether the ABEND occur in a layer of system software.
4317+
4318+
These ABEND codes would occasionally be accompanied by a reason code which can be utilised to further narrow down the possible cause of errors.
4319+
4320+
- **S001** - Record Length / Block Size Discrepancy
4321+
- **S013** - Conflicting DCB Parameters
4322+
- **S0C1** - Invalid Instruction
4323+
- **S0C4** - Storage Protection Exception
4324+
- **S0C7** - Data Exception
4325+
- **S0CB** - Division by Zero
4326+
- **S222/S322** - Time Out / Job Cancelled
4327+
- **S806** - Module Not Found
4328+
- **B37/D37/E37** - Dataset or PDS Index Space Exceeded
4329+
4330+
In the next sections, we will go through the ABENDs along with any possible reasons and the frequent causes of the ABENDs. Note that the reasons and causes are non-exhaustive.
4331+
4332+
### S001 - Record Length / Block Size Discrepancy
4333+
4334+
z/OS manages data using data sets, which is a file that contains one or more records. These data sets have a predetermined record length and a maximum length of a block of storage (block size) associated with them during their creation. Most of the time the discrepancy happens due to programming errors.
4335+
4336+
**Reason Codes:**
4337+
- S001-0: Conflict between record length specification (program vs JCL vs dataset label)
4338+
- S001-2: Damaged storage media or hardware error
4339+
- S001-3: Fatal QSAM error
4340+
- S001-4: Conflict between block specifications (program vs JCL)
4341+
- S001-5: Attempt to read past end-of-file
4342+
4343+
**Frequent Causes:**
4344+
- S001-0: Typos in the FD statement or JCL
4345+
- S001-2: Corrupt disk or tape dataset
4346+
- S001-3: Internal z/OS problem
4347+
- S001-4: Forgot to code BLOCK CONTAINS 0 RECORDS in the FD statement
4348+
- S001-5: Logic error
4349+
4350+
### S013 - Conflicting DCB Parameters
4351+
4352+
S013 ABEND occurs when the program is expecting the Data Definition (DD) statement to have a specific Data Control Block (DCB), but the DD have a different DCB. Again this can be something like block size, record length, or record format.
4353+
4354+
To read more on data sets, visit the IBM Knowledge Center:
4355+
4356+
[https://www.ibm.com/docs/en/zos-basic-skills?topic=more-what-is-data-set](https://www.ibm.com/docs/en/zos-basic-skills?topic=more-what-is-data-set)
4357+
4358+
**Reason Codes:**
4359+
- S013-10: Dummy data set needs buffer space; specify BLKSIZE in JCL
4360+
- S013-14: DD statement must specify a PDS
4361+
- S013-18: PDS member not found
4362+
- S013-1C: I/O error in searching the PDS directory
4363+
- S013-20: Block size is not a multiple of the record length
4364+
- S013-34: Record length is incorrect
4365+
- S013-50: Tried to open a printer for an input
4366+
- S013-60: Block size not equal to record length for unblocked size
4367+
- S013-64: Attempted to dummy out indexed or relative file
4368+
- S013-68: Block size is larger than 32752
4369+
- S013-A4: SYSIN or SYSOUT is not QSAM file
4370+
- S013-A8: Invalid record format for SYSIN or SYSOUT
4371+
- S013-D0: Attemped to define PDS with FBS or FS record format
4372+
- S013-E4: Attemped to concatenate more than 16 PDSs
4373+
4374+
**Frequent Causes:**
4375+
Most of the reason for this ABEND code is due to inconsistencies between the JCL and the COBOL program.
4376+
4377+
### S0C1 - Invalid Instruction
4378+
4379+
In S0C1, the CPU is attempting to execute an instruction that is either invalid or not supported.
4380+
4381+
**Reasons:**
4382+
- SYSOUT DD statement missing
4383+
- The value in an AFTER ADVANCING clause is less than 0 or more than 99
4384+
- An index or subscript is out of range
4385+
- An I/O verb was issued against an unopened data set
4386+
- CALL subroutine linkage does not match the calling program record definition
4387+
4388+
**Frequent Causes:**
4389+
- Incorrect logic in setting AFTER ADVANCING clause
4390+
- Incorrect logic in table handling code, or an overflow of table entries
4391+
4392+
### S0C4 - Storage Protection Exception
4393+
4394+
When you run your COBOL program in z/OS, the operating system will allocate a block of virtual memory which is called address space. The address space will contain memory addresses that are necessary for the execution of the program.
4395+
4396+
**Reason:**
4397+
With S0C4, the program is attempting to access a memory address that is not within the address space allocated.
4398+
4399+
**Frequent Causes:**
4400+
- Missing or incorrect JCL DD statement
4401+
- Incorrect logic in table handling code
4402+
- Overflow of table entries
4403+
- INITIALIZE a file FD that hasn't been opened
4404+
4405+
### S0C7 - Data Exception
4406+
4407+
As you have seen previously, COBOL program handles data using PICTURE clauses, which determine the type of data that particular variable. But occasionally, you may encounter data that are misplaced.
4408+
4409+
**Reason:**
4410+
With S0C7, the program is expecting numeric data, however, it found other invalid types of data. This can happen when you try to MOVE something non-numeric from a PIC 9 field to a PIC X field.
4411+
4412+
**Frequent Causes:**
4413+
- Incorrectly initialized or uninitialized variables
4414+
- Missing or incorrect data edits
4415+
- MOVE from a 01-level to a 01-level if the sending field is shorter than receiving field
4416+
- MOVE of zeros to group-level numeric fields
4417+
- Incorrect MOVE CORRESPONDING
4418+
- Incorrect assignment statements when MOVE from one field to another
4419+
4420+
### S0CB - Division by Zero
4421+
4422+
Just like mathematic, attempting to divide a number with 0 in Enterprise COBOL is an undefined operation.
4423+
4424+
**Reason:**
4425+
CPU attempted to divide a number with 0.
4426+
4427+
**Frequent Causes:**
4428+
- Incorrectly initialized or uninitialized variables
4429+
- Missing or incorrect data edits
4430+
4431+
### S222/S322 - Time Out / Job Cancelled
4432+
4433+
When you submit a JCL, it is possible to determine how much time you want to allocate to a job. If the job surpasses that allocated time, it will time out. Depending on how your system is set up, a job that has taken a prolonged time may be cancelled either manually by the operator or automatically.
4434+
4435+
**Reason:**
4436+
Timeout, likely due to program logic getting caught in a loop with no possible exit (infinite loop). To be specific, S322 ABEND refers to timeout, while S222 refer to the job being cancelled.
4437+
4438+
**Frequent Causes:**
4439+
- Invalid logic
4440+
- Invalid end-of-file logic
4441+
- End-Of-File switch overwritten
4442+
- Subscript not large enough
4443+
- PERFORM THRU a wrong exit
4444+
- PERFORM UNTIL End-Of-File without changing the EOF switch
4445+
4446+
### S806 - Module Not Found
4447+
4448+
We have seen previously that it is possible to CALL a subroutine in COBOL. To allow the compiler to know what subroutine we want to call, we need to specify them on the JCL. If you do not indicate them, the compiler will attempt to check the system libraries first before failing.
4449+
4450+
**Reason:**
4451+
CALL made to a subroutine that could not be located.
4452+
4453+
**Frequent Causes:**
4454+
- Module deleted from the library
4455+
- Module name spelt incorrectly
4456+
- Load library with the module is not specified on the JCL
4457+
- I/O error when z/OS searched the directory of the library
4458+
4459+
### B37/D37/E37 - Dataset or PDS Index Space Exceeded
4460+
4461+
We have seen that data set in z/OS have an allocated size to them. When we create many data, at one point the data set won't have enough space to store anything new.
4462+
4463+
**Reason Codes:**
4464+
- B37 - Disk volume out of space
4465+
- D37 - Primary space exceeded, no secondary extents defined
4466+
- E37 - Primary and secondary extents full
4467+
- E37-04 - Disk volume table of contents is full
4468+
4469+
**Frequent Causes:**
4470+
- Not enough space to allocate the output file(s)
4471+
- Logic error resulting in an infinite write loop
4472+
4473+
## Best Practices to Avoid ABEND
4474+
4475+
To avoid ABEND, we can do something called defensive programming. It is a form of programming where we defensively design our code to ensure that it is still running under unforeseen circumstances.
4476+
4477+
By doing defensive programming, we can reduce the number of bugs and make the program more predictable regardless of the inputs.
4478+
4479+
Listed below are some things we can do in COBOL:
4480+
4481+
- **INITIALIZE fields at the beginning of a routine.** This will ensure that the field has proper data at the start of the program. However, special care needs to be taken to ensure that any flags or accumulators have the appropriate INITIALIZE data.
4482+
4483+
- **I/O statement checking.** This can be through the use of FILE STATUS variable and checking them before doing any further I/O operation. Additionally, we need to check for empty files and other possible exceptions.
4484+
4485+
- **Numeric fields checking.** A general policy would be to not trust a numeric field we are doing math on. Assume that the input can be invalid. It would be recommended to use ON OVERFLOW and ON SIZE ERROR phrases to catch invalid or abnormal data. Special care should be taken when we need to do rounding as truncation can occur in some cases.
4486+
4487+
- **Code formatting.** This will ensure that your code is maintainable and easy to understand by anyone who is reading or maintaining them.
4488+
4489+
- **Consistent use of scope terminators.** It would be best practice to explicitly terminate a scope using scope terminators such as END-IF, END-COMPUTE or END-PERFORM.
4490+
4491+
- **Testing, Checking and Peer-Review.** Proper tests and peer-review can be conducted to catch possible errors that may have slipped through your program. Additionally, we can also ensure that the business logic is correct.
4492+
4493+
## ABEND Routines
4494+
4495+
Even when a system ABEND does not occur, there are possible situations where you will be expected to call an ABEND routine. This could be when you encounter invalid input data for your program or an error being returned from a subroutine.
4496+
4497+
Usually, such routines would be supplied by your place of employment. But it can be as simple as the following example:
4498+
4499+
```
4500+
IF abend-condition
4501+
PERFORM ABEND-ROUTINE.
4502+
...
4503+
ABEND-ROUTINE.
4504+
DISPLAY "Invalid data".
4505+
STOP RUN.
4506+
```
4507+
4508+
Such routine can display more information which would allow you to determine where and why exactly has the program failed.

0 commit comments

Comments
 (0)