Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


How can I turn on hardware execptions and trap them?

Posted on 2006-06-19
Medium Priority
Last Modified: 2010-04-24
How can I turn on hardware exceptions?  The test procedure of a cacluation library tries to force a floating point exception by throwing a whole slew of bogus numbers at the calculation functions.  I want to trap, and find instances where this test procedure is successful in causing the calculation library to do something which is ordinarily a floating point exception; i.e. divide by zero, sqrt(-1), log(-100), asin (5), etc. etc.

Is there a way to turn these on, and for the test program to detect their occurrence.  I just got bit by a divide by zero problem that was in the code and didn't show up until the code was run on another platform (the client's platform of course).  I'd like to trap these errors on my system, MS Visual C++ .NET (currently 2003), in test mode, before the client sees them.  Thanks in advance.

Question by:doneDad
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 15

Expert Comment

ID: 16942061
1. Specify floating point behaviour as described in
2. Have your code inside try, catch blocks and handle the Arithmetic exceptions.
 Please refer to
for more info.

Author Comment

ID: 16944962
Thanks for the info, this looks promising.  I'll try this out this evening.  The System.DivideByZeroException . . . et al look like managed code exceptions types.  Are there similar types for unmanaged exceptions?  Will plain old std::execption work?
LVL 15

Expert Comment

ID: 16950677
I don't think you can handle using std::exception

It was a specific design decision of C++ not to handle divide-by-zero; So you must check your divisors yourself, or discover if your hardware maps divide-by-zero onto some other kind of exception and catch that. It's fairly easy to discover the latter. Just put a try {} catch (...) {} around a divide by zero.

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


Author Comment

ID: 16953333
Well, I'm not even getting that far.  I have VC++ 7.0 (2002) and VC++7.1 (2003) and neither will accept /fp as a compiler options.  Neither help file lists /fp as an option.  Am I out of luck here?  Is there some special magic that you have to use?

If I do a catch (...) how do I discover what has been thrown, since I don't have a variable name to work with?
LVL 15

Accepted Solution

lakshman_ce earned 1000 total points
ID: 16955089
I have found /fp either. I think /fp may be in VS2005. But sample code like the following will catch the divide by zero exception

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

int _tmain()
    // TODO: Please replace the sample code below with your own.
    //Console::WriteLine(S"Hello World");
      int i=1;
      int j=0;
            int k= i/j;
      catch(System::DivideByZeroException *exp)
      return 0;

Yes I agree catch(...) wouldn't give why it happened. But would say some unknown exception occured.

Please note that Dividing a floating-point value by zero will result in either positive infinity, negative infinity, or Not-a-Number (NaN) according to the rules of IEEE 754 arithmetic. Floating-point operations never throw an exception


Author Comment

ID: 16980667
We'll be upgrading to MSVC++ 2005 soon, hoefully the /fp option will be there and we'll be able to use that option at that time.  Until then, we'll simply have to test the resulting calcuation values for NAN and infinity.  I have found a librarty function to do that, _finite ().  It checks (supposedly) for both inifitiy and NAN.

Many thanks for your help.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question