Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ : Catch a divide by zero error

Here is a simple piece of code where a division by zero occurs. I'm trying to catch it :

#include <iostream>  int main(int argc, char *argv[]) {     int Dividend = 10;     int Divisor = 0;      try {         std::cout << Dividend / Divisor;     } catch(...) {         std::cout << "Error.";     }     return 0; } 

But the application crashes anyway (even though I put the option -fexceptions of MinGW).

Is it possible to catch such an exception (which I understand is not a C++ exception, but a FPU exception) ?

I'm aware that I could check for the divisor before dividing, but I made the assumption that, because a division by zero is rare (at least in my app), it would be more efficient to try dividing (and catching the error if it occurs) than testing each time the divisor before dividing.

I'm doing these tests on a WindowsXP computer, but would like to make it cross platform.

like image 435
Jérôme Avatar asked Jan 20 '11 13:01

Jérôme


People also ask

How do you find the divide by zero error?

Examples. The following example handles a DivideByZeroException exception in integer division. using System; public class Example { public static void Main() { int number1 = 3000; int number2 = 0; try { Console. WriteLine(number1 / number2); } catch (DivideByZeroException) { Console.

What happens if you divide by zero in C?

Because what happens is that if we can say that zero, 5, or basically any number, then that means that that "c" is not unique. So, in this scenario the first part doesn't work. So, that means that this is going to be undefined. So zero divided by zero is undefined.

Why is dividing by zero an error?

The reason that the result of a division by zero is undefined is the fact that any attempt at a definition leads to a contradiction. a=r*b. r*0=a. (1) But r*0=0 for all numbers r, and so unless a=0 there is no solution of equation (1).

Is divide by zero a trap?

When divide by zero operation is performed, a trap is generated i.e. INT0 is sent to the processor and ultimately SIGFPE signal is sent to the process that performed the operation.


1 Answers

It's not an exception. It's an error which is determined at hardware level and is returned back to the operating system, which then notifies your program in some OS-specific way about it (like, by killing the process).

I believe that in such case what happens is not an exception but a signal. If it's the case: The operating system interrupts your program's main control flow and calls a signal handler, which - in turn - terminates the operation of your program.

It's the same type of error which appears when you dereference a null pointer (then your program crashes by SIGSEGV signal, segmentation fault).

You could try to use the functions from <csignal> header to try to provide a custom handler for the SIGFPE signal (it's for floating point exceptions, but it might be the case that it's also raised for integer division by zero - I'm really unsure here). You should however note that the signal handling is OS-dependent and MinGW somehow "emulates" the POSIX signals under Windows environment.


Here's the test on MinGW 4.5, Windows 7:

#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; } 

Output:

Signal 8 here!

And right after executing the signal handler, the system kills the process and displays an error message.

Using this, you can close any resources or log an error after a division by zero or a null pointer dereference... but unlike exceptions that's NOT a way to control your program's flow even in exceptional cases. A valid program shouldn't do that. Catching those signals is only useful for debugging/diagnosing purposes.

(There are some useful signals which are very useful in general in low-level programming and don't cause your program to be killed right after the handler, but that's a deep topic).

like image 80
Kos Avatar answered Sep 28 '22 04:09

Kos