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

How can I catch a zero-delimited error (and not other errors and access exception information) in Visual Studio 2008 C ++?

I tried this:

try { int j=0; int i= 1/j;//actually, we call a DLL here, which has divide-by-zero } catch(std::exception& e){ printf("%s %s\n", e.what()); } catch(...){ printf("generic exception"); } 

But this applies to the general catch block. I understand that MS-specific __try may be useful here somehow, but I would prefer standard C ++, and in any case I have destructors that prevent the use of __try.

CONFIRMATION: The above code is simplified for discussion. Actually, dividing by zero is an error that occurs deep in a third-party DLL for which I have no source code. The error depends on the parameter (handle of complex structure), which I pass to the library, but not in an obvious way. So, I want to be able to gracefully restore.

+7
c ++ exception-handling visual-studio-2008 try-catch
source share
8 answers

Assuming you can't just fix the reason for generating the exception code (perhaps because you don't have the source code for this particular library and maybe because you can't configure the input options before they cause the problem) .

You need to jump over a few hoops to do this job on your own, but it can be done.

First you need to set up the structured exception translation function by calling _set_se_translator() (see here ), then you can examine the code that you're passed when the SEH exception is raised and the corresponding C ++ exception is thrown.

 void CSEHException::Translator::trans_func( unsigned int code, EXCEPTION_POINTERS *pPointers) { switch (code) { case FLT_DIVIDE_BY_ZERO : throw CMyFunkyDivideByZeroException(code, pPointers); break; } // general C++ SEH exception for things we don't need to handle separately.... throw CSEHException(code, pPointers); } 

Then you can just catch CMyFunkyDivideByZeroException() in C ++ in the usual way.

Please note that you need to set the exception translation function for each thread that you want to translate as exceptions.

+7
source share

To catch the division by null exceptions in Visual C ++ try-> catch (...), just enable / EHa in the project settings. See Project Properties β†’ C / C ++ β†’ Code Generation β†’ Change C ++ Exception Exceptions to β€œYes with SEH Exceptions” . What is it!

See here for more details: http://msdn.microsoft.com/en-us/library/1deeycx5 (v = vs .80) .aspx

+10
source share

C ++ does not treat divide-by-zero as an exception, per-se.

Quote from Stroustrup:

β€œLow-level events, such as overflow and division by zero arithmetic, are assumed to be lower-level mechanisms rather than exceptions. This allows the use of C ++ behavior in other languages ​​when it comes to arithmetic. It also avoids problems that occur on pipelined architectures where events such as division by zero are asynchronous. "

"Design and Evolution of C ++" (Addison Wesley, 1994)

In any case, exceptions are never a substitute for the proper processing of preconditions.

+9
source share

You can use structured exception handling (using __try, etc.), or you can set up a structured exception handler: _ set_se_translator

Both of these are operating system specific.

+7
source share

You cannot do this using standard C ++, as this is not a standard C ++ exception. This is a structured exception. For a standard C ++ exception, someone should throw exception; from code.

+4
source share

Why not check it out earlier? Performance will be trivial for simple j == 0 compared to context switching for exception handling.

+3
source share

Try using the following code:

 try { const int j=0; if (j == 0) { throw std::exception("j was 0"); } const int i= 1/j; } catch(std::exception& e) { printf("%s %s\n", e.what()); } catch(...) { printf("generic exception"); } 

Of course, if you do this in order without exception, you can do:

 const int j = 0; if (j == 0) { /* do something about the bad pre-condition here */ } else { const int i = 1 / j; } 

Edit in response to the explanation: you will need to find out what contribution is what you give to the third party, which forces them to divide by zero in front of you, and process this before calling their function.

+2
source share

A good approach would be to use secure object-oriented wrappers such as SafeInt . It also seems to be integrated in Visual Studio 2010.

update:
If division by zero occurs in third-party code, the only option is SEH or something equivalent as Seb Rose answers .

+1
source share

All Articles