Fortran

Guide To Learn

Error handling and closing the file

In our quick-note app, we haven’t had an explicit need to close the file because we’re prompting the user for input and writing to the file in an infinite loop. The only way for the program to quit is by the user pressing Ctrl+C. However, if any of the I/O statements encounters an error (such as no space on disk, for example), the program should be able to print a helpful error message and close the file gracefully. The changes needed to enable error handling in the quick-note app are shown in the following listing.

Listing 6.13 Catching and recovering from errors in I/O statements

integer :: stat
...
do
  read(stdin, '(a)', iostat=stat, err=100) text    ❶
  write(fileunit, '(a)', iostat=stat, err=100) &   ❶
    trim(text)                                     ❶
  flush(fileunit, iostat=stat, err=100)            ❶
end do
 
100 close(fileunit)                                ❷
if (stat > 0) then
  write(stderr, '(a, i3)') &                       ❸
    'Error encountered, code = ', stat             ❸
  stop                                             ❸
end if
...

❶ Gets the status number and jumps to the label if an error is encountered

❷ The program will jump to this point on error.

❸ Prints the message with the error code

The main additions to the code are the iostat and err keyword parameters to each of the readwrite, and flush statements. They enable Fortran’s built-in error handling for I/O statements. Each of the readwriteopencloseinquireflush, and rewind statements allows passing the iostat and err keyword parameters:

  • iostat–The integer status code, which evaluates to 0 if no error is encountered, and a positive number otherwise
  • err–An integer error label to which the program will jump if the error is encountered

For brevity and demonstration, we’re only checking for errors on readwrite, and flush statements. If an error is encountered in any of them, the program control is transferred to the label (the line of code that begins with a number, in this case 100) and the program continues from there. It will first use the close statement to close the file, then print the error message with the error code if the code is nonzero. The meaning of each error code isn’t defined by the Standard and is specific to each compiler, so you’d need to consult the compiler documentation for details. Note that without error handling, I/O errors are always nonrecoverable, meaning that the program will stop, usually with a descriptive error message, although this is also compiler-specific.

It’s good practice to close the file when we’re done working with it. This ensures that any data left over in the I/O buffer gets written to disk before the program ends.

With error handling and closing the file, this concludes our minimal note-taking app. For your reference, you can find the complete code in the listings repository on GitHub (https://github.com/modern-fortran/listings) in src/ch06/qn.f 90.

Error handling and closing the file

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top