Typically in an error situation, your program will stop, and you'll get an error message. The only exception to this is that at the end of
write statements' parenthesized control list, you can add,
err=label to determine which line to jump to in the event of an error.
Modern Fortran (from Fortran 90 onwards) has introduced four main areas for error capture:
1) File handling and i/o operation error handling
2) IEEE floating point error detection and reporting
3) Dynamic allocation
4) Command line operations
File handling and I/O OperationsEdit
All the external file handling statements and I/O operations (
wait) can now take optional
iostat is an integer which returns a non-zero value if there is an error, in which case, the character variable assigned to
iomsg will return a brief error message. The non-zero integers and the messages are compiler dependent but the intrinsic module,
iso_fortran_env, gives access to two important values:
iostat_eor. If an error occurs, and
iostat is non-zero, execution will not stop. The ERR clause is still supported but should not be used.
integer :: my_iostat character (256) :: my_iomsg open (file='my.dat', unit=10, iostat=my_iostat, iomsg=my_iomsg) if (my_iostat/=0) then write (*,*) 'Open my.dat failed with iostat = ', my_iostat, ' iomsg = '//trim(my_iomsg) end if
Note that the length required for the message character is vendor and error dependent.
IEEE floating point error detection and reportingEdit
This is a big topic, but in essence modern Fortran provides access to three intrinsic modules:
IEEE_features. These features can be used to intercept errors such as divide by zero and overflow but at the expense of some performance.
IEEE_features module controls access to the features the programmer may require, by use association in the scoping unit where the programmer places the use statement,
subroutine blah use, intrinsic :: ieee_features ! ... end subroutine blah
See Chapter 11 in Metcalf et al, Modern Fortran Explained, OUP. All the necessary basic facilities exist in order for the programmer to construct a try/catch system if desired.
Modern Fortran allows run-time allocation and deallocation of arrays of any type, and a typical error might be to try to dynamically allocate an array so large that there is not enough memory, or an attempt to deallocate an array which is not already allocated. There are optional clauses
errmsg which can be used to prevent program failure and allow the programmer to take evasive action.
real, allocatable, dimension (:) :: x integer :: my_stat character (256) :: my_errmsg allocate (x(100000000), stat=my_stat, errmsg=my_errmsg) if (my_stat/=0) then write(*,*) 'Failed to allocate x with stat = ', my_stat, ' and errmsg '//trim(my_errmsg) end if
These features are available in the equivalent coarray features.
Command Line OperationsEdit
Modern Fortran also supports error detection for the execution of command line operations,
integer :: my_cmdstat character (256) :: my_cmdmsg call execute_command_line('my.exe', cmdstat=my_cmdstat, cmdmsg=my_cmdmsg ) if (my_cmdstat/=0) stop
In this example, the programmer of the my.exe program has the responsibility for what codes are returned and what error messages are exposed, except that -1 and -2 are reserved for allowing the compiler vendor indicating what features may be supported.