Sun Studio 12: Fortran Programming Guide

6.4 Debugging IEEE Exceptions

Locating where the exception occurred requires exception trapping be enabled. This can be done by either compiling with the -ftrap=common option (the default when compiling with f95) or by establishing an exception handler routine with ieee_handler(). With exception trapping enabled, run the program from dbx, using the dbx catch FPE command to see where the error occurs.

The advantage of compiling with -ftrap=common is that the source code need not be modified to trap the exceptions. However, by calling ieee_handler() you can be more selective as to which exceptions to look at.

Example: Compiling for and using dbx:


demo% f95 -g myprogram.f
demo% dbx a.out
Reading symbolic information for a.out
...
(dbx) catch FPE
(dbx) run
Running: a.out
(process id 19739)
signal FPE (floating point divide by zero) in MAIN at line 212 in file "myprogram.f"
  212               Z = X/Y
(dbx)  print Y
y = 0.0
(dbx)

If you find that the program terminates with overflow and other exceptions, you can locate the first overflow specifically by calling ieee_handler() to trap just overflows. This requires modifying the source code of at least the main program, as shown in the following example.

Example: Locate an overflow when other exceptions occur:


demo% cat myprog.F
#include “floatingpoint.h”
        program myprogram
...
      ier = ieee_handler(”set’,’overflow’,SIGFPE_ABORT)
...
demo% f95 -g myprog.F
demo% dbx a.out
Reading symbolic information for a.out
...
(dbx) catch FPE
(dbx) run
Running: a.out
(process id 19793)
signal FPE (floating point overflow) in MAIN at line 55 in file "myprog.F"
   55               w = rmax * 200.                     ! Cause of the overflow
(dbx) cont                                   ! Continue execution to completion
execution completed, exit code is 0
(dbx)

To be selective, the example introduces the #include, which required renaming the source file with a .F suffix and calling ieee_handler(). You could go further and create your own handler function to be invoked on the overflow exception to do some application-specific analysis and print intermediary or debug results before aborting.