Wednesday 15 September 2010

How to catch divide-by-zero error in Visual Studio 2008 C++? -


How can I find a divider-zero-zero error (and other errors and being able to access exception information ) In Visual Studio 2008 C ++?

I tried to do this:

  try {int j = 0; Int i = 1 / j; // In fact, we call a DLL here, which has a split-rate-zero} hold (std :: exception & e) {printf ("% s% s \ n", e.what ()) ; } Hold (...) {printf ("general exception"); }  

But this is normal ... hold blocks. I think MS-specific __trree may be useful here, but I prefer standard C ++, and in any case I have destructors who stop the use of __try.

Clarification: The above code is simple. For discussion purposes, actually, by dividing zero, there is a bug which is deep in the third party DLL for which I do not have the source code. Depending on the error parameter (a handle for a complex structure), which I pass to the library, but not in any clear way. Therefore, I want to be able to honestly recover.

Assuming that you can not just fix the cause of the exception generated code (maybe because your There is no source code in that particular library and probably because before you can not adjust the input parameters before causing the problem).

Some hoops for you to do this work as you want, but it can be done.

First of all you need to set up a structured exception translation function handling by calling _set_se_translator () (see) then you can check the code that you pass When an SEH is an exception and can throw an appropriate C ++ exception

  zero CSEHException :: translator :: trans_func (unsigned int code, EXCEPTION_POINTERS * pPointers) {switch (code) {case FLT_DIVIDE_BY_ZERO: CMyFunkyDivideByZeroException (code, pPointers) Throw; break; } // The usual C ++ SEH exception for things we do not need to handle separately .... Throw CSEHException (code, pPointers); }  

So you can just hold your CMyFunkyDivideByZeroException () in normal way in C ++.

Note that you need to set your exception translation function on every thread you want to translate the exception.


No comments:

Post a Comment