Specifies the action to take when program execution is interrupted.
Syntax
ON interrupt-condition action |
- interrupt-condition
is the interrupt to be handled, either an arithmetic
error or a keyboard interrupt.
- action
is one of the following:
where:
trap-routine
is an external subroutine name.
Description
The ON
statement is an HP extension. It is an executable statement that
specifies the action to be taken after the occurrence of an exception
that interrupts program execution.
For each interrupt-condition, you
can specify one of the following actions:
CALL:
specifies a subroutine to be called.
ABORT:
causes the program to abort.
IGNORE:
causes the interrupt to be ignored.
Table 10-2 “Exceptions handled by the
ON statement” lists the range
of values for interrupt-condition. The
first column identifies the type of trap; the second gives the keywords
that must appear on the ON statement,
immediately following the word ON;
and the third column gives equivalent keywords you can specify instead
of those in the second column. For example, the following ON
statement causes the program to trap an attempt to divide by zero
with 8-byte floating-point operands, passing control to a user-written
trap handler called div_zero_trap:
ON REAL(8) DIV 0 CALL trap_div_by_zero |
The following ON statement
does the same thing, but it specifies the equivalent keywords from
the third column of the table:
ON DOUBLE PRECISION DIV 0 CALL trap_div_by_zero |
Table 10-2 Exceptions handled by the
ON statement
Exceptions | Exception keywords | Alternate
keywords |
|---|
Division
by zero | REAL(4) DIV 0 | REAL DIV 0 |
| REAL(8) DIV 0 | DOUBLE PRECISION DIV 0 |
| REAL(16) DIV 0 | (none) |
| INTEGER(2) DIV 0 | INTEGER*2 DIV 0 |
| INTEGER(4) DIV 0 | INTEGER DIV 0 |
Overflow | REAL(4) OVERFLOW | REAL OVERFLOW |
| REAL(8) OVERFLOW | DOUBLE PRECISION OVERFLOW |
| REAL(16) OVERFLOW | (none) |
| INTEGER(2) OVERFLOW | INTEGER*2 OVERFLOW |
| INTEGER(4) OVERFLOW | INTEGER OVERFLOW |
Underflow | REAL(4) UNDERFLOW | REAL UNDERFLOW |
| REAL(8) UNDERFLOW | DOUBLE PRECISION UNDERFLOW |
| | REAL(16) UNDERFLOW | (none) |
Invalid
(illegal) operation | REAL(4) ILLEGAL | REAL ILLEGAL |
| REAL(8) ILLEGAL | DOUBLE PRECISION ILLEGAL |
| REAL(16) ILLEGAL | (none) |
Inexact
result | REAL(16) INEXACT | (none) |
| REAL(4) INEXACT | REAL INEXACT |
| REAL(8) INEXACT | DOUBLE PRECISION INEXACT |
Control-C | CONTROLC | (none) |
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 90 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 90 Programmer's Guide
provides detailed information about using the ON
statement, including example programs that use the ON
statement.