You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: COBOL Programming Course #1 - Getting Started/COBOL Programming Course #1 - Getting Started.md
+238Lines changed: 238 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4375,3 +4375,241 @@ Previous lab programs made use of a date/time intrinsic function. The date/time
4375
4375
**Lab Hints**
4376
4376
4377
4377
Refer to CBL0011 line 120 for the proper formatting of the function-name causing the compile error.
4378
+
4379
+
\newpage
4380
+
# ABEND handling
4381
+
4382
+
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.
4383
+
4384
+
This chapter introduces ABEND and gives an overview of frequent ABEND types which a COBOL application programmer may encounter. We will review possible reasons and frequent causes of the ABEND types for the programmer to debug. We will also review some common best practices to avoid ABEND and review reasons why a programmer may purposedly call an ABEND routine in their application.
4385
+
4386
+
4387
+
-**Why does ABEND happen?**
4388
+
4389
+
-**Frequent ABEND Types**
4390
+
4391
+
-**S001 - Record Length / Block Size Discrepancy**
4392
+
4393
+
-**S013 - Conflicting DCB Parameters**
4394
+
4395
+
-**S0C1 - Invalid Instruction**
4396
+
4397
+
-**S0C4 - Storage Protection Exception**
4398
+
4399
+
-**S0C7 - Data Exception**
4400
+
4401
+
-**S0CB - Division by Zero**
4402
+
4403
+
-**S222/S322 - Time Out / Job Cancelled**
4404
+
4405
+
-**S806 - Module Not Found**
4406
+
4407
+
-**B37/D37/E37 - Dataset or PDS Index Space Exceeded**
4408
+
4409
+
-**Best Practices to Avoid ABEND**
4410
+
4411
+
-**ABEND Routines**
4412
+
4413
+
4414
+
## Why does ABEND happen?
4415
+
4416
+
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.
4417
+
4418
+
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.
4419
+
4420
+
## Frequent ABEND Types
4421
+
4422
+
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.
4423
+
4424
+
These ABEND codes would occasionally be accompanied by a reason code which can be utilised to further narrow down the possible cause of errors.
4425
+
4426
+
-**S001** - Record Length / Block Size Discrepancy
4427
+
-**S013** - Conflicting DCB Parameters
4428
+
-**S0C1** - Invalid Instruction
4429
+
-**S0C4** - Storage Protection Exception
4430
+
-**S0C7** - Data Exception
4431
+
-**S0CB** - Division by Zero
4432
+
-**S222/S322** - Time Out / Job Cancelled
4433
+
-**S806** - Module Not Found
4434
+
-**B37/D37/E37** - Dataset or PDS Index Space Exceeded
4435
+
4436
+
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.
4437
+
4438
+
### S001 - Record Length / Block Size Discrepancy
4439
+
4440
+
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.
4441
+
4442
+
**Reason Codes:**
4443
+
- S001-0: Conflict between record length specification (program vs JCL vs dataset label)
4444
+
- S001-2: Damaged storage media or hardware error
4445
+
- S001-3: Fatal QSAM error
4446
+
- S001-4: Conflict between block specifications (program vs JCL)
4447
+
- S001-5: Attempt to read past end-of-file
4448
+
4449
+
**Frequent Causes:**
4450
+
- S001-0: Typos in the FD statement or JCL
4451
+
- S001-2: Corrupt disk or tape dataset
4452
+
- S001-3: Internal z/OS problem
4453
+
- S001-4: Forgot to code BLOCK CONTAINS 0 RECORDS in the FD statement
4454
+
- S001-5: Logic error
4455
+
4456
+
### S013 - Conflicting DCB Parameters
4457
+
4458
+
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.
4459
+
4460
+
To read more on data sets, visit the IBM Knowledge Center:
- S013-10: Dummy data set needs buffer space; specify BLKSIZE in JCL
4466
+
- S013-14: DD statement must specify a PDS
4467
+
- S013-18: PDS member not found
4468
+
- S013-1C: I/O error in searching the PDS directory
4469
+
- S013-20: Block size is not a multiple of the record length
4470
+
- S013-34: Record length is incorrect
4471
+
- S013-50: Tried to open a printer for an input
4472
+
- S013-60: Block size not equal to record length for unblocked size
4473
+
- S013-64: Attempted to dummy out indexed or relative file
4474
+
- S013-68: Block size is larger than 32752
4475
+
- S013-A4: SYSIN or SYSOUT is not QSAM file
4476
+
- S013-A8: Invalid record format for SYSIN or SYSOUT
4477
+
- S013-D0: Attemped to define PDS with FBS or FS record format
4478
+
- S013-E4: Attemped to concatenate more than 16 PDSs
4479
+
4480
+
**Frequent Causes:**
4481
+
Most of the reason for this ABEND code is due to inconsistencies between the JCL and the COBOL program.
4482
+
4483
+
### S0C1 - Invalid Instruction
4484
+
4485
+
In S0C1, the CPU is attempting to execute an instruction that is either invalid or not supported.
4486
+
4487
+
**Reasons:**
4488
+
- SYSOUT DD statement missing
4489
+
- The value in an AFTER ADVANCING clause is less than 0 or more than 99
4490
+
- An index or subscript is out of range
4491
+
- An I/O verb was issued against an unopened data set
4492
+
- CALL subroutine linkage does not match the calling program record definition
4493
+
4494
+
**Frequent Causes:**
4495
+
- Incorrect logic in setting AFTER ADVANCING clause
4496
+
- Incorrect logic in table handling code, or an overflow of table entries
4497
+
4498
+
### S0C4 - Storage Protection Exception
4499
+
4500
+
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.
4501
+
4502
+
**Reason:**
4503
+
With S0C4, the program is attempting to access a memory address that is not within the address space allocated.
4504
+
4505
+
**Frequent Causes:**
4506
+
- Missing or incorrect JCL DD statement
4507
+
- Incorrect logic in table handling code
4508
+
- Overflow of table entries
4509
+
- INITIALIZE a file FD that hasn't been opened
4510
+
4511
+
### S0C7 - Data Exception
4512
+
4513
+
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.
4514
+
4515
+
**Reason:**
4516
+
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.
4517
+
4518
+
**Frequent Causes:**
4519
+
- Incorrectly initialized or uninitialized variables
4520
+
- Missing or incorrect data edits
4521
+
- MOVE from a 01-level to a 01-level if the sending field is shorter than receiving field
4522
+
- MOVE of zeros to group-level numeric fields
4523
+
- Incorrect MOVE CORRESPONDING
4524
+
- Incorrect assignment statements when MOVE from one field to another
4525
+
4526
+
### S0CB - Division by Zero
4527
+
4528
+
Just like mathematics, attempting to divide a number with 0 in Enterprise COBOL is an undefined operation.
4529
+
4530
+
**Reason:**
4531
+
CPU attempted to divide a number with 0.
4532
+
4533
+
**Frequent Causes:**
4534
+
- Incorrectly initialized or uninitialized variables
4535
+
- Missing or incorrect data edits
4536
+
4537
+
### S222/S322 - Time Out / Job Cancelled
4538
+
4539
+
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.
4540
+
4541
+
**Reason:**
4542
+
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.
4543
+
4544
+
**Frequent Causes:**
4545
+
- Invalid logic
4546
+
- Invalid end-of-file logic
4547
+
- End-Of-File switch overwritten
4548
+
- Subscript not large enough
4549
+
- PERFORM THRU a wrong exit
4550
+
- PERFORM UNTIL End-Of-File without changing the EOF switch
4551
+
4552
+
### S806 - Module Not Found
4553
+
4554
+
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.
4555
+
4556
+
**Reason:**
4557
+
CALL made to a subroutine that could not be located.
4558
+
4559
+
**Frequent Causes:**
4560
+
- Module deleted from the library
4561
+
- Module name spelt incorrectly
4562
+
- Load library with the module is not specified on the JCL
4563
+
- I/O error when z/OS searched the directory of the library
4564
+
4565
+
### B37/D37/E37 - Dataset or PDS Index Space Exceeded
4566
+
4567
+
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.
4568
+
4569
+
**Reason Codes:**
4570
+
- B37 - Disk volume out of space
4571
+
- D37 - Primary space exceeded, no secondary extents defined
4572
+
- E37 - Primary and secondary extents full
4573
+
- E37-04 - Disk volume table of contents is full
4574
+
4575
+
**Frequent Causes:**
4576
+
- Not enough space to allocate the output file(s)
4577
+
- Logic error resulting in an infinite write loop
4578
+
4579
+
## Best Practices to Avoid ABEND
4580
+
4581
+
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.
4582
+
4583
+
By doing defensive programming, we can reduce the number of bugs and make the program more predictable regardless of the inputs.
4584
+
4585
+
Listed below are some things we can do in COBOL:
4586
+
4587
+
-**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.
4588
+
4589
+
-**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.
4590
+
4591
+
-**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.
4592
+
4593
+
-**Code formatting.** This will ensure that your code is maintainable and easy to understand by anyone who is reading or maintaining them.
4594
+
4595
+
-**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.
4596
+
4597
+
-**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.
4598
+
4599
+
## ABEND Routines
4600
+
4601
+
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.
4602
+
4603
+
Usually, such routines would be supplied by your place of employment. But it can be as simple as the following example:
4604
+
4605
+
```
4606
+
IF abend-condition
4607
+
PERFORM ABEND-ROUTINE.
4608
+
...
4609
+
ABEND-ROUTINE.
4610
+
DISPLAY "Invalid data".
4611
+
STOP RUN.
4612
+
```
4613
+
4614
+
Such routine can display more information which would allow you to determine where and why exactly has the program failed.
0 commit comments