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:
To transfer control to error-handling code within the program, use the ERR, EOR, and END branch specifiers in I/O statements.
To identify Fortran-specific I/O errors based on the value of Intel Fortran RTL error codes, use the I/O status specifier (IOSTAT) in I/O statements (or call the ERRSNS subroutine).
Obtain system-level error codes by using the appropriate library routines.
For certain error conditions, use the signal handling facility to change the default action to be taken.
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.
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:
The END branch specifier handles an end-of-file condition.
The EOR branch specifier handles an end-of-record condition for nonadvancing reads.
The ERR branch specifier handles all error conditions.
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:
Modify the error-handling routine to display the error message number
Remove the END, EOR, or ERR branch specifiers from the I/O statement that causes the error
After you modify the source code, compile, link, and run the program to display the error message. For example:
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:
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.
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:
A value of -2 if an end-of-record condition occurs with nonadvancing reads.
A value of -1 if an end-of-file condition occurs.
A value of 0 for normal completion (not an error condition, end-of-file, or end-of-record condition).
A positive integer value if an error condition occurs. (This value is one of the Fortran-specific IOSTAT numbers listed in the run-time error message. See Run-Time Error Messages.
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_80/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):
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)
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
WRITE (6,*) 'File not found. Type ls to find file and run again'
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).