HP Fortran Programmer's Guide (March 2010)
Using the ON statement
Actions specified by ON
Chapter 5 137
z = x/y
PRINT *, z
END PROGRAM main
As defined by the IEEE standard, a floating-point operation that results in a NaN is an
exception known as an invalid operation. The example program performs an invalid
operation when it passes a negative argument to the LOG intrinsic, causing the intrinsic to
return a NaN. The following ON statement:
ON REAL INVALID IGNORE
causes the program to ignore this exception and continue execution.
The program also attempts to divide by zero. Although the ON statement enables the trap
triggered by a divide-by-zero exception, the statement has no other effect. As a result, the
exception will cause the program to abort. To ignore the divide-by-zero exception would
require an additional ON statement:
ON REAL DIV 0 IGNORE
Here is command line to compile the program, followed by the output from a sample run:
$ f90 ignore.f90
$ a.out
NaN
PROGRAM ABORTED : IEEE divide by zero
PROCEDURE TRACEBACK:
( 0) 0x00002504 _start + 0xbc [./a.out]
Calling a trap procedure
You can write trap procedures that are callable by the ON statement to handle arithmetic
errors in user code and in library routines. Trap procedures can take zero or one argument. If
an argument is specified, it is the result and must have the type specified by the exception
keyword. For example, if the following ON statement occurs in a program:
ON DOUBLE PRECISION OVERFLOW CALL trap
then the procedure trap could declare one argument of type DOUBLE PRECISION. Note that
the argument is optional. Also, depending on the exception, the contents of the argument may
not always be meaningful.
The following sections discuss two example programs that use the ON statement to call a trap
procedure for floating-point exception and for an integer exception.