The USE statement is used to code the procedure to be executed when an exception occurs. (For this example an input-output error is used.)
000010 @OPTIONS MAIN 000020*---------------------------------------------------------------------- 000030* In this sample, the USE statement is coded to capture input-output 000035* errors. 000040*---------------------------------------------------------------------- 000050 IDENTIFICATION DIVISION. 000060 PROGRAM-ID. SAMPLE. 000070 ENVIRONMENT DIVISION. 000080 INPUT-OUTPUT SECTION. 000090 FILE-CONTROL. 000100 SELECT TMP-FILE ASSIGN TO "C:\NONFILE" 000110 FILE STATUS IS F-STAT. 000120 DATA DIVISION. 000130 FILE SECTION. 000140 FD TMP-FILE. 000150 01 TMP-REC PIC X(80). 000160 WORKING-STORAGE SECTION. 000170 01 F-STAT. 000180 02 STAT-1 PIC X(1). 000190 02 STAT-2 PIC X(1). 000200 PROCEDURE DIVISION. 000210*---------------------------------------------------------------------- 000220* The input-output error procedure (USE statement) is coded in the 000225* declarative section. 000230*---------------------------------------------------------------------- 000240 DECLARATIVES. 000250*---------------------------------------------------------------------- 000260* Here, the input-output error procedure is defined for TMP-FILE. 000270* If an input-output error occurs for TMP-FILE when this procedure is 000280* present, control will be passed to the start of this procedure 000290* without issuing a run-time system error. 000300*---------------------------------------------------------------------- 000310 IO-ERR SECTION. 000320 USE AFTER EXCEPTION PROCEDURE TMP-FILE. 000330 EVALUATE F-STAT 000340 WHEN "35" 000350 DISPLAY "The file cannot be found." 000360 WHEN "47" 000370 DISPLAY "The READ statement was executed for an unopened file." 000380 WHEN "42"
000390 DISPLAY "The CLOSE statement was executed for an unopened file." 000400 WHEN OTHER 000410 DISPLAY "Another input-output error occurred." 000420 END-EVALUATE. 000430 DISPLAY ">>>> Processing continues. ". 000440 END DECLARATIVES. 000450*---------------------------------------------------------------------- 000460 OPEN INPUT TMP-FILE. 000460 READ TMP-FILE AT END GO TO P-EXIT. 000480 CLOSE TMP-FILE. 000490 P-EXIT. 000500 EXIT PROGRAM. 000510 END PROGRAM SAMPLE.