HP Fortran Programmer's Reference (September 2007)

HP Fortran statements
ON (extension)
Chapter 10 409
To use the ON statement to trap for integer overflow, you must also include the $HP$
CHECK_OVERFLOW directive. This is described in the HP Fortran Programmer’s Guide.
Using the ON statement at optimization levels 2 and above is restricted. When compiling at
optimization level 2 or above, the optimizer makes assumptions about the program that do
not take into account the behavior of procedures called by the ON statement. Such procedures
must therefore be “well-behaved”—in particular, they must meet the following criteria:
The ON procedure must not assume that any variable in the interrupted procedure or in
its caller has its current value. (The optimizer may have placed the variable in a register
to be stored there until after the call to the interrupted procedure is complete.)
The ON procedure must not change the value of any variable in the interrupted procedure
or in its caller if the effect of the ON procedure is to return program control to the point of
interrupt.
NOTE If you include the ON statement in a program that is compiled at optimization
level 2 or higher and the program takes an exception, the results may vary from
those you would get from the unoptimized program or from the same program
without the ON statement.
Examples
The following example uses the ON statement to call the procedure trap_div_by_zero if the
function do_div is passed 0 in argument y. If trap_div_by_zero is called, it prints an error
message and assigns 0 to the result.
REAL FUNCTION do_div(x, y)
REAL :: x, y
ON REAL DIV 0 CALL trap
do_div = x/y ! causes an interrupt if y = 0
RETURN
END FUNCTION do_div
SUBROUTINE trap(res)
REAL :: res
PRINT *, "Don’t do that."
res = 0
END SUBROUTINE trap
Related concepts
The HP Fortran Programmer’s Guide provides detailed information about using the ON
statement, including example programs that use the ON statement.