Fortran Programming Guide

Detecting an Exception by Handler

The following examples show how to create handler routines to detect floating-point exceptions.

Example: Detect exception and abort:


demo% cat DetExcHan.f
      EXTERNAL myhandler                         
      REAL r / 14.2 /, s / 0.0 / 
      i = ieee_handler ('set', 'division', myhandler ) 
      t = r/s 
      END

      INTEGER FUNCTION myhandler(sig,code,context)
      INTEGER sig, code, context(5) 
      CALL abort() 
      END 
demo% f77 -silent DetExcHan.f 
demo% a.out 
abort: called 
Abort (core dumped) 
demo%

SIGFPE is generated whenever that floating-point exception occurs. When the SIGFPE is detected, control passes to the myhandler function, which immediately aborts. Compile with -g and use dbx to find the location of the exception.