I can not catch exception and, accordingly, debug a program in C ++ ...

try { int a = 1, b = 0; int c = a / b; // в данном случае имитирую exception для примера } catch (...) { int a = 1; } 

I used to catch a breakpoint in catch and could catch exception. Why is it now fails. Maybe it's in the IDE settings? I use Visual Studio 2013

  • Most likely the "garbage" in the handler, the optimizer simply throws out garbage, and do not get into the exception. int a = 1; considered garbage, because the declared variable is not used anywhere. - nick_n_a
  • one
    Like dividing by 0 is no exception C ++ - Vladimir Gamalyan
  • If you throw garbage, you would get an empty catch (...) {} - just exit the program. And it hangs in my try {} - sitev_ru
  • It is impossible to divide by 0, of course this exception ... - sitev_ru
  • And yet - in the studio there is an opportunity to catch an exception at the moment of its occurrence (see Exception Settings), this is more convenient than putting a bryak in a catch body, since immediately visible the place where it happened. - Vladimir Gamalyan

3 answers 3

C ++ exceptions and processor exceptions are completely different things. So you will not catch anything. You need Structured Exception Handling - something like this:

 int main(int argc, const char * argv[]) { __try { int a = 1; int c = a / (argc - 1); // в данном случае имитирую exception для примера cout << c << endl; } __except(GetExceptionCode() == EXCEPTION_INT_DIVIDE_BY_ZERO) { cout << "Exception!\n"; } } 

Well, if you need to generate exactly a C ++ exception - then there is a throw for it - just write something like throw(0); ...

  • Never write something like throw(0) . - αλεχολυτ
  • @alexolut I am returning your comment to you: if a person intends to intercept catch(...) immediately, and then with a view to debugging, and knows for sure that he will not do anything else, then ... :) - Harry

The answer to your question is in English SO. Here is a small translation of the best answer:


The fact is that according to the language standard, division by zero is not considered an exceptional situation, but it generates an error that is determined at the hardware level and returns back to the operating system, which then notifies your program in a specific OS in a specific way (for example, ending the process).

I believe that in this case, it is not an exception, but a signal. If so, the Operating System interrupts the main control flow of your program and calls the signal handler, which - in turn - terminates the program.

The same type of error occurs when you dereference a null pointer and your program crashes on a SIGSEGV signal, which causes a segmentation error.

You can use the functions from the <csignal> header to make a custom handler for the SIGFPE signal (which is used to exclude a floating-point number, but it can be used for integer division by zero - But in fact, I'm not very sure about that). However, it should be noted that signal processing is OS-dependent and MinGW somehow "emulates" POSIX signals in the Windows environment.


 #include <csignal> #include <iostream> using namespace std; void handler(int a) { cout << "Signal " << a << " here!" << endl; } int main() { signal(SIGFPE, handler); int a = 1/0; } 

conclusion

Signal 8 here!

  • one
    There are no signals in Windows - ixSci
  • just the code in windows launched everything works - perfect
  • This is an emulation of several signals from POSIX, but there are no signals in Windows. The code may work, but the explanation in the answer is incorrect - there are no signals in windows. - ixSci
  • one
    The correct answer is given in the Harry reply - in Windows, such things are handled correctly using SEH; this is the canonical way for windows. - ixSci
  • I think the author of the answers uses the word signal in an abstract sense. Since the emulation library exists, why not use this word? - perfect

I found a solution here: stackoverflow.com/a/8226455/3909255

You need to enable the /EHa option in the Visual Studio project settings. Look at Project PropertiesC / C ++Code Generation , change the Enable C ++ Exceptions to "Yes With SEH Exceptions".

  • And how can we distinguish the division of a 0 from, for example, the assignment of a null pointer? ... - Harry
  • The main thing for me is to detect the line with an error, and already in it it is clear what is wrong ... - sitev_ru
  • Strange that the correct answer scored negative votes))) - sitev_ru
  • 2
    Not all RuSO users are fluent in English. If you think that the answer to EnSO answers the question, make a translation and provide a link to the source. - rdorn
  • one
    The answer is correct, but "dumb". Even MSDN does not recommend using it in general ... - Harry