< Fortran

Fortran 77 was not real big on catching errors. 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 READ and WRITE statements' parenthesized control list, you can add , ERR=lineNumber 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 Operations

All the external file handling statements and I/O operations (OPEN, READ, WRITE, CLOSE, INQUIRE, BACKSPACE, ENDFILE, FLUSH, REWIND and WAIT) can now take optional IOSTAT and IOMSG clauses

IOSTAT is an integer which returns non-zero if there is an error, in which case, if present, the character variable assigned to IOMSG will return a more or less helpful 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_end and iostat_eor. If an error occurs, and the IOSTAT clause is present, execution will not stop. The ERR clause is still supported but should not be used.

Example

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)

...

Note that the length required for the message character is vendor and error dependent.

IEEE floating point error detection and reporting

This is a big topic, but in essence modern Fortran provides access to three intrinsic modules: IEEE_arithmetic, IEEE_exceptions and IEEE_features. These features can be used to intercept errors such as divide by zero and overflow but at the expense of some performance.

The 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,

Example

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.

Dynamic Allocation

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 STAT and ERRMSG which can be used to prevent program failure and allow the programmer to take evasive action.

Example

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)

...

These features are available in the equivalent coarray features.

Command Line Operations

Modern Fortran also supports error detection for the execution of command line operations,

Example

integer :: my_cmdstat

character (256) :: my_cmdmsg

...

call EXECUTE_COMMAND_LINE('my.exe', cmdstat=my_cmdstat, cmdmsg=my_cmdmsg )

if(my_cmdstat /= 0) then

...

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.

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.