tags:

views:

329

answers:

2

Does anyone have an idea how you can catch the exception that cobol throws if you try to open an IO file if it doesn't exist, and then create a new file?

+1  A: 

Hi,

I don't know what version of Cobol you use or what platform you use it on. My program checks first to see if the file exists before it tries to open it. I use Unisys Cobol 85 on the MCP mainframe platform. The messages are lame, but who cares?

Here is a snippet from a job that runs daily:

968545     IF  ATTRIBUTE RESIDENT OF OU3-WORK-LIST-FILE = VALUE TRUE  
968550         DISPLAY "PROGRAM SHOWS ATTRIBUTE TRUE"                 
968555         OPEN EXTEND OU3-WORK-LIST-FILE                         
968560     ELSE                                                       
968565         DISPLAY "PROGRAM SHOWS FALSE"                          
968570         OPEN OUTPUT OU3-WORK-LIST-FILE                         
968575     END-IF.                                                    
968580

Cathy

Cathy Sullivan
+3  A: 

The OPTIONAL phrase on the SELECT cause will do this:

SELECT OPTIONAL FILE-A 
   ASSIGN TO "INFILE" 
   ORGANIZATION INDEXED.

If OPEN IO the file will be created if necessary. For OPEN INPUT, the file not be created but treated as being at EOF and all random reads will be "INVALID KEY".

I'm pretty sure this is an ANSI standard clause, but can't remember when it showed up.

Dave Smith
Good answer - I believe the OPTIONAL clause is specified in the COBOL-85 standard. If you are working on z/os, you need to add the CBLQDA(ON) runtme option (the default is OFF). Beware, this option is not available under CICS.
NealB