Handling errors in joining and splitting strings
During the joining or splitting of strings, the pointer
used by STRING or UNSTRING might
fall outside the range of the receiving field. A potential overflow
condition exists, but COBOL does not let the overflow happen.
About this task
Instead, the STRING or UNSTRING operation
is not completed, the receiving field remains unchanged, and control
passes to the next sequential statement. If you do not code the ON
OVERFLOW phrase of the STRING or UNSTRING statement,
you are not notified of the incomplete operation.
Consider the following statement:
String Item-1 space Item-2 delimited by Item-3
into Item-4
with pointer String-ptr
on overflow
Display "A string overflow occurred"
End-String
These are the data values before and after the statement is performed:
| Data item | PICTURE |
Value before | Value after |
|---|---|---|---|
Item-1 |
X(5) |
AAAAA | AAAAA |
Item-2 |
X(5) |
EEEAA | EEEAA |
Item-3 |
X(2) |
EA | EA |
Item-4 |
X(8) |
bbbbbbbb1 | bbbbbbbb1 |
String-ptr |
9(2) |
0 | 0 |
| 1. The symbol b represents a blank space. | |||
Because String-ptr has a value (0) that
falls short of the receiving field, an overflow condition occurs and
the STRING operation is not completed. (Overflow
would also occur if String-ptr were greater than
9.) If ON OVERFLOW had not been specified, you would
not be notified that the contents of Item-4 remained
unchanged.