Using the IOSTAT Specifier and Fortran Exit Codes

You can use the IOSTAT specifier to continue program execution after an I/O error and to return information about I/O operations. Certain errors are not returned in IOSTAT.

The IOSTAT specifier can supplement or replace the END, EOR, and ERR branch transfers.

Execution of an I/O statement containing the IOSTAT specifier suppresses the display of an error message and defines the specified integer variable, array element, or scalar field reference as one of the following, which is returned as an exit code if the program terminates:

Following the execution of the I/O statement and assignment of an IOSTAT value, control transfers to the END, EOR, or ERR statement label, if any. If there is no control transfer, normal execution continues.

You can include the for_iosdef.for file in your program to obtain symbolic definitions for the values of IOSTAT.

The following example uses the IOSTAT specifier and the for_iosdef.for file to handle an OPEN statement error (in the FILE specifier).

Error Handling OPEN Statement File Name

     CHARACTER(LEN=40) :: FILNM     INCLUDE 'for_iosdef.for'     DO I=1,4       FILNM = ''       WRITE (6,*)  'Type file name '       READ (5,*) FILNM       OPEN (UNIT=1, FILE=FILNM, STATUS='OLD', IOSTAT=IERR, ERR=100)       WRITE (6,*) 'Opening file: ', FILNM !       (process the input file)       CLOSE (UNIT=1)       STOP  100  IF (IERR .EQ. FOR$IOS_FILNOTFOU) THEN         WRITE (6,*) 'File: ', FILNM, ' does not exist '       ELSE IF (IERR .EQ. FOR$IOS_FILNAMSPE) THEN          WRITE (6,*) 'File: ', FILNM, ' was bad, enter new file name'       ELSE         PRINT *, 'Unrecoverable error, code =', IERR         STOP       END IF     END DO     WRITE (6,*) 'File not found. Locate correct file with Explorer and run again'   END PROGRAM

Another way to obtain information about an error is the ERRSNS subroutine, which allows you to obtain the last I/O system error code associated with an Intel Fortran RTL error (see the Intel® Fortran Language Reference).