5 Using the ON statement

Whenever a runtime error occurs, the default action of your program depends on the type of the error. If the error results from a floating-point exception, the program will continue to execute. Other errors will cause it to abort.

As described in “Handling runtime exceptions” (page 77), the +fp_exception and +FPoptions provide control over how a program behaves when a runtime error occurs. The ON statement provides an additional level of control by enabling your program to handle floating-point and integer exceptions and +Ctrl-Cinterrupts. Before an exception can be handled, the flow of control must pass through an ON statement that specifies:

The type of the exception

One of the following actions:

Execute a trap procedure

Ignore the interrupt

Abort the program

The action specified by the ON statement can only be changed by another ON statement that specifies the same exception.

This chapter describes how to use the ON statement. The syntax of the ON statement is described in the HPFortran Programmer’s Reference. For detailed information about trapping math errors, see the HP-UXFloating-Point Guide.

NOTE: If you include theON statement in a program that you optimize at level 2 or higher and the program takes an exception, the results may vary from those you would get from an unoptimized program or from a program that didn’t have the ONstatement.

Exceptions handled by the ON statement

Like the +fp_exceptionoption, the ONstatement enables traps for floating-point exceptions (by default, traps for floating-point exceptions are disabled on HP 9000 computers). When traps are enabled, an executing program that takes any of the following exceptions will abort, unless an ONstatement specifies a different action:

Division by zero

Overflow

Underflow

Invalid (or illegal) operation

These exceptions are defined by the IEEE standard for floating-point operations. The ONstatement enables traps for these exceptions, regardless of whether the exception is taken by user code or by a call to a library routine. In addition, the ON statement also enables traps for integer division by zero, integer overflow, and +Ctrl-Cinterrupts. The +Ctrl-Cinterrupt occurs when the user presses +Ctrl-Cduring program execution.

Table5-1 on page131 lists the exceptions handled by the ONstatement and gives the keywords that must be specified in the ONstatement to indicate the exception being handled. The first column indicates the type of exception. The second column gives the keywords that must appear in the ON statement, immediately following the word ON. The third column gives alternate keywords you can specify instead of those in the second column.

For example, the following ONstatement will trap attempts to divide by zero with 8-byte floating-point operands:

ON REAL(8) DIV 0 CALL div_zero_trap

Exceptions handled by the ON statement 81