HP Fortran Programmer's Guide (March 2010)

Using the ON statement
Actions specified by ON
Chapter 5136
! ON REAL DIV 0 ABORT
x = 10.0
y = 0.0
z = x / y
PRINT *, y
END PROGRAM main
Here is the command line and the output from a sample run:
$ f90 abort.f90
$ a.out
PROGRAM ABORTED : IEEE divide by zero
PROCEDURE TRACEBACK:
( 0) 0x0000248c _start + 0x6c [./a.out]
The program would have the same result if you were to comment out the ON statement and
compile with the +fp_exception option.
Ignoring errors
You can use the ON statement to ignore an exception by specifying the IGNORE keyword. The
following paragraphs discuss an example program, ignore.f90, that uses the ON statement to
ignore an invalid operation. The following program illustrates this.
Example 5-2 ignore.f90
PROGRAM main
REAL :: x, y, z
! The following ON statement enables traps for floating-point
! exceptions and causes the program to ignore an invalid
! operation exception.
ON REAL ILLEGAL IGNORE
! The next two statements pass a negative argument to the LOG
! intrinsic, resulting in an invalid operation. This
! exception is ignored, as specified by the ON statement.
x = -10.0
y = LOG(x)
PRINT *, y
! The next three statements attempt to divide by zero. The
! trap for this exception is enabled by the previous
! ON statement but no action is specified. Therefore,
! the program will abort execution.
x = 9.0
y = 0