[Insight-developers] Floating Point Exception Handling in ITK -- your feedback needed.

kent williams norman-k-williams at uiowa.edu
Wed Jul 14 11:06:59 EDT 2010


I have an itk::FloatingPointExceptions class now that on OS X and Linux that
turns on numerical exceptions.  It's simple enough on the outside, and not
that complicated on the inside; but before I check it in I wanted to make
sure that it passed stylistic muster with you lot.

If you enable numerical exceptions with this class, the default behavior is
to call abort().  This is consistent (I think) with the Borland 6 behavior.

The SetExceptionAction method allows you to specify whether to call exit or
abort when an exception is thrown.  This is only part of the class for the
benefit of CTest:  You can have tests in CMake that pass only when the test
returns non-zero (i.e. fails), but if a test program aborts, that counts as
a test failure even if you set the property WILL_FAIL to TRUE for that test.

This class, in a default Insight build, does nothing. You have to set
ITK_USE_FLOATINGPOINTEXCEPTIONS to ON when you configure a build to turn on
floating point exceptions.  If this CMAKE variable is turned off, there's no
change at all to the ITK library's current behavior.

The itkFloatingPointExceptions.cxx contains specific code for OS X Intel
(tested), OS X PPC (not tested), Linux (tested), and Windows (not tested or
fully implemented).

The Windows code does turn on exceptions, but doesn't have a SIGFPE handler
to exit or abort on exceptions.  I don't know if there's  a special  way to
set this up on Windows, and don't have a Windows machine set up to build and
test that.  I assume that implementing this on Windows would be
straightforward, modulo Microsoft's usual obtuseness.

The FloatingPointExceptions is a singleton -- its only members are static,
and it can't be instantiated.


/** \class itkFloatingPointExceptions
 * Allows floating point exceptions to be caught during program execution.
 */
class ITK_EXPORT FloatingPointExceptions
{
public:
  /** defines what should happen when exceptions occur */
  typedef enum { ABORT, EXIT } ExceptionAction;
  /** Enable floating point exceptions */
  static void Enable();
  

  /** Disable floating point exceptions. */
  static void Disable();
  /** Control whether exit(255) or abort() is called on an exception */
  static void SetExceptionAction(ExceptionAction a);
  /** Access current ExceptionAction */
  static ExceptionAction GetExceptionAction();
private:
  FloatingPointExceptions(); // Not implemented.
  FloatingPointExceptions(const FloatingPointExceptions&);  // Not
                                                            // implemented.
  void operator=(const FloatingPointExceptions&);  // Not implemented.
  /** static member that controls what happens during an exception */
  static ExceptionAction m_ExceptionAction;
};




More information about the Insight-developers mailing list