1

What is the error handling command for open input line-sequential file?

I tried,

OPEN INPUT CUSTOMER-FILE
INVALID KEY/ON ERROR
DISPLAY "NO FILE FOUND".

but could not get it to work.

Thanks.

Jack011
  • 59
  • 1
  • 9
  • Seems like only yesterday that I commented on this :-) I personally favour the FILE STATUS over Declaratives or Imperatives. There is no imperative available on OPEN (or CLOSE). – Bill Woodger Aug 08 '14 at 13:33

2 Answers2

3

In FILE-CONTROL, add a FILE STATUS clause to the SELECT for your file and in the PROCEDURE DIVISION check the value of the file status variable you specified against the documented values after each file interaction.

cschneid
  • 10,237
  • 1
  • 28
  • 39
  • @BillWoodger Any example guys? I don't really understand. – Jack011 Aug 08 '14 at 16:07
  • 1
    You have a SELECT in your program, which ties your files internally to your files externally. A part of the SELECT is the FILE STATUS (it is optional in COBOL but mandatory for me). This names a two-byte field which you define in WORKING-STORAGE. After each IO statement, that field will get set to a value representing the result of the operation. 00 is good. 10 for a read of an input file is end-of-file, stuff like that. It is all documented in your Micro Focus manuals. Have a look here, for instance: http://supportline.microfocus.com/documentation/books/sx20books/fhfsta.htm – Bill Woodger Aug 08 '14 at 16:11
  • @BillWoodger - you're right, and I've now removed the last sentence. – cschneid Aug 09 '14 at 16:03
0

I agree with using a file status, it is much more flexible and you can handle all errors in one solution. Here is most of the code you need for it:

FILE-CONTROL.                                                
    SELECT  FILENAME  ASSIGN  TO  FILENAME                 
                        FILE STATUS IS WS-FS-FILENAME.       
*I  used WS-FS-FILENAME so I know it's declared in Working Storage and connected to File *Section.

DATA DIVISION.                                               
FILE SECTION.                                                

FD  FILENAME            RECORDING  ....                         
                        BLOCK      ...                        
                        RECORD     ....                       
                        LABEL      RECORD    ....        
                        DATA       RECORD    ...
                        .....  

01  FILENAME-REC         PIC X(N).




WORKING-STORAGE SECTION.                                       
01  WS-FS-FILENAME   PIC XX.   

==================================================


OPEN OUTPUT FILENAME.                        
IF WS-FS-FILENAME NOT = '00' THEN            
   DISPLAY 'ERROR OPENING FILENAME'          
   DISPLAY 'ERROR CODE IS : ', WS-FS-FILENAME
   PERFORM EXIT-WITH-ERROR                        
END-IF.                                     

WRITE FILENAME-REC.                                   
IF WS-FS-FILENAME NOT = '00'                          
   DISPLAY 'WRITE ERROR ON FILENAME. ' 
   DISPLAY 'STATUS :' WS-FS-FILENAME                  
   PERFORM EXIT-WITH-ERROR                                
END-IF. 

And so on with reading it, closing it.

You can find the specific error codes in the Cobol documentation as well.

bmakos
  • 159
  • 8