HP-UX Floating-Point Guide

Chapter 6 157
Floating-Point Trap Handling
Handling Traps
Handling Traps
Once you have enabled traps, either by a compiler option or by a call to a
routine, you need a mechanism for handling them when they occur. It
may be convenient simply to have your program abort (particularly if you
enable traps with a method that does not cause a core dump). You may,
however, prefer to establish an error-handling routine that generates a
helpful error message and exits the program gracefully.
HP 9000 systems provide the following methods of handling traps:
The ON statement (Fortran only)
The sigaction(2) function (C only)
We discuss these briefly in the following subsections.
If you want to continue execution after handling a trap, do so with
caution. The result of the calculation that encountered the exception is
likely to be useless, and you will probably want to substitute for it a
carefully chosen value that will be meaningful in subsequent operations.
Be particularly cautious about continuing execution if you use the C
sigaction(2) function for error handling. Floating-point exceptions are
hardware exceptions, whereas an error-handling process (such as the C
sigaction(2) function or the HP Fortran ON statement) occurs in software.
A hardware exception usually causes an operation to be interrupted at a
point when values are stored in registers but not yet stored in memory. If
you use sigaction(2) to try to substitute a new value for the one that
caused the error, chances are you will return to a point later in the
instruction sequence than the point at which the error occurred, so some
essential steps may be eliminated and the register values may be invalid.
This particular problem does not occur with the HP Fortran ON
statement; the software updates the registers appropriately.
Using the ON Statement (Fortran only)
The ON statement may be used to handle arithmetic exceptions in both
HP Fortran 90 and HP FORTRAN/9000. In HP FORTRAN/9000 the
statement must be used in conjunction with the +T compiler option. In
HP Fortran 90 it may be used in conjunction with the +fp_exception
option, but this is not required.