Big news! The community will be moving to a new platform April 21. Read more.
Big news! The community will be moving to a new platform April 21. Read more.
Ensign
Ensign
89 views

How to get Visual Cobol to fail for divide by zero and subscript out of range errors?

Hello, 

I run this program:

       identification division.
       program-id. DivideByZeroTest.
 
       environment division.
       configuration section.
 
       data division.
       working-storage section.
       01 ws-vars.
           05 ws-zero                  pic 999 value 0.
           05 ws-one                   pic 999 value 1. 
           05 ws-result                pic 999 value 0.
       01 ws-tbl.
           05 ws-tbl-items             pic 999 occurs 2.
       procedure division.
       000-main.
           move 0 to ws-result.
           display "ws-zero   =" ws-zero   "=,"
                   "ws-one    =" ws-one    "=,"
                   "ws-result =" ws-result "=".
      *
           compute ws-result = ws-one / ws-zero. 
           display "after compute ws-result = ws-one / ws-zero".
           display "ws-zero   =" ws-zero   "=,"
                   "ws-one    =" ws-one    "=,"
                   "ws-result =" ws-result "=".
      *
           move 0 to ws-result.
           display "reset ws-result =" ws-result "=".
      *
           divide ws-one by ws-zero giving ws-result. 
           display "after "
                   "divide ws-one by ws-zero giving ws-result".
           display "ws-zero   =" ws-zero   "=,"
                   "ws-one    =" ws-one    "=,"
                   "ws-result =" ws-result "=".
      *
           move 0 to ws-result.
           display "reset ws-result =" ws-result "=".
      *
           compute ws-result = 1/0.
           display "after "
                   "compute ws-result = 1/0".
           display "ws-zero   =" ws-zero   "=,"
                   "ws-one    =" ws-one    "=,"
                   "ws-result =" ws-result "=".
      *
           display "tbl test start".
           display "before move 0 to index 0".
           move 0 to ws-tbl-items(0).
           display "ws-tbl-items(0)=" ws-tbl-items(0) "=".
           display "before move 1 to index 1".
           move 1 to ws-tbl-items(1).
           display "ws-tbl-items(1)=" ws-tbl-items(1) "=".
           display "before move 2 to index 2".
           move 2 to ws-tbl-items(2).
           display "ws-tbl-items(2)=" ws-tbl-items(2) "=".
           display "before move 3 to index 3".
           move 3 to ws-tbl-items(3).
           display "ws-tbl-items(3)=" ws-tbl-items(3) "=".
           display "tbl test done".
      *
           display "press any key to continue".
           accept return-code.
           goback.
      *
       end program DivideByZeroTest.
 
I get no divide by zero and subscript out of range errors, 
and program executes with the displays shown below on console.
 
VS2019 does report several warnings in lines 69-79 for:
Boundary violation. Processed as written.
 
Console:
 
ws-zero   =000=,ws-one    =001=,ws-result =000=
after compute ws-result = ws-one / ws-zero
ws-zero   =000=,ws-one    =001=,ws-result =001=
reset ws-result =000=
after divide ws-one by ws-zero giving ws-result
ws-zero   =000=,ws-one    =001=,ws-result =001=
reset ws-result =000=
after compute ws-result = 1/0
ws-zero   =000=,ws-one    =001=,ws-result =000=
tbl test start
before move 0 to index 0
ws-tbl-items(0)=000=
before move 1 to index 1
ws-tbl-items(1)=001=
before move 2 to index 2
ws-tbl-items(2)=002=
before move 3 to index 3
ws-tbl-items(3)=003=
tbl test done
press any key to continue

Any suggestions would be helpful. 
I am teaching Cobol at my college, and want to explain to my students what is happening here, 
and how to get the program to fail with these error conditions. 
I have tried researching this online, but have yet to find any clear approach to a solution to use.

 

After some more research, it seems that 
$SET CHECKDIV"ENTCOBOL" triggers runtime divide by zero errors. 

I am confused why this is not the default for divide by zero runtime errors in general.  
Very confusing for new programmers when code works, but not really.

As for the subscript out of range, 
$SET SSRANGE
or 
$SET BOUND
These seem to imply they should help, but neither triggers a runtime error in my sample code. 

I also tried $SET DIALECT"ENTCOBOL" and this was not helpful either. 

I am confused why this is not the default for subscript out of range runtime errors in general.
Very confusing for new programmers when code works, but not really.

Regards, 
Gregory Oakes
0 Likes
4 Replies
Commander
Commander

You should be using indexes, however with variable length records sending the subscript negative can read the record length (binary number). A trick used on OS/390 mainframes. For subscript out of range just do a simple if test for < 1 or > max items.

 

 

0 Likes
Ensign
Ensign

Seems strange to code for ws-tbl-item (0) or ws-tbl-item (3) being subscript out of range when ws-tbl-item PIC 99 occurs 2? The compiler and runtime system must know 0 and 3 are invalid subscripts.
You do not have to check denominators = 0 before every divide? There is an option to force divide by zero errors 'CHECKDIV', so why not an option the check subscripts? Still not clear why this option is not on by default, but at least it exists.
0 Likes
Micro Focus Expert
Micro Focus Expert

The BOUND directive is turned on by default and this will check for invalid subscript errors only if a data-item is being used as the subscript. This will trigger a RTS error 153.

You are using literals as subscripts line 0 and 3 and these will generate a compiler warning instead which basically tells you to fix the problem before you run the code.

Example:

If you change the line:

move 3 to ws-tbl-items(3).
to
move 3 to ws-tbl-items(my-num).

where my-num is defined in working-storage as:

01 my-num pic 9 value 3.

Then a RTS error 153 will be generated.

0 Likes
Ensign
Ensign

Thank you for the info about subscripts being variable versus literal being handled differently for the subscript out of range condition.  Where is this documented, so I can read more about it?

What other compiler-directive options are turned on by default?
Where is this documented, so I can read more about it?

I have tried searching for the detail on both of these without much success.

thanks.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.