Solved

How can I turn on hardware execptions and trap them?

Posted on 2006-06-19
6
162 Views
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.

0
Comment
Question by:doneDad
  • 3
  • 3
6 Comments
 
LVL 15

Expert Comment

by:lakshman_ce
Comment Utility
1. Specify floating point behaviour as described in
http://msdn2.microsoft.com/en-us/library/e7s85ffb.aspx
2. Have your code inside try, catch blocks and handle the Arithmetic exceptions.
  System.ArithmeticException
         System.DivideByZeroException
         System.NotFiniteNumberException
         System.OverflowException
 Please refer to
http://msdn2.microsoft.com/en-us/system.arithmeticexception.aspx
for more info.
0
 

Author Comment

by:doneDad
Comment Utility
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?
0
 
LVL 15

Expert Comment

by:lakshman_ce
Comment Utility
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.

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:doneDad
Comment Utility
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?
0
 
LVL 15

Accepted Solution

by:
lakshman_ce earned 250 total points
Comment Utility
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;
      try
      {
            int k= i/j;
      }
      catch(System::DivideByZeroException *exp)
      {
            Console::WriteLine(exp->Message);
      }
      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

0
 

Author Comment

by:doneDad
Comment Utility
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.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now