Handling Run-Time Errors

Whenever possible, the Intel Fortran RTL does certain error handling, such as generating appropriate messages and taking necessary action to recover from errors. You can explicitly supplement or override default actions by using the following methods:

These error-processing methods are complementary; you can use any or all of them within the same program to obtain Intel Fortran run-time and Linux* system error codes.

Using the END, EOR, and ERR Branch Specifiers

When a severe error occurs during Intel Fortran program execution, the default action is to display an error message and terminate the program. To override this default action, there are three branch specifiers you can use in I/O statements to transfer control to a specified point in the program:

If you use the END, EOR, or ERR branch specifiers, no error message is displayed and execution continues at the designated statement, usually an error-handling routine.

You might encounter an unexpected error that the error-handling routine cannot handle. In this case, do one of the following:

After you modify the source code, compile, link, and run the program to display the error message. For example:

READ (8,50,ERR=400)

If any severe error occurs during execution of this statement, the Intel Fortran RTL transfers control to the statement at label 400. Similarly, you can use the END specifier to handle an end-of-file condition that might otherwise be treated as an error. For example:

READ (12,70,END=550)

When using nonadvancing I/O, use the EOR specifier to handle the end-of-record condition. For example:

150 FORMAT (F10.2, F10.2, I6)

  READ (UNIT=20, FMT=150, SIZE=X, ADVANCE='NO', EOR=700) A, F, I

You can also use ERR as a specifier in an OPEN, CLOSE, or INQUIRE statement. For example:

OPEN (UNIT=10, FILE='FILNAM', STATUS='OLD', ERR=999)

If an error is detected during execution of this OPEN statement, control transfers to the statement at label 999.

Using the IOSTAT Specifier

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:

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 /opt/intel/fc/9.0/include/for_iosdef.for 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):

     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. Type ls to find file 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).