0

If I have to call a particular function in which devide by zero is happening. We cannot modify anything in called function. Only way to prevent is by doing some modifiaction in calling function. How t do that?

for ex:

//calling function

int func(5,1);

//called function

int func(int x,int y)
{
  y--;
 return(x/y); // here divide by zero will occur, but we cannot do any thing in 
              // called function
}

How to keep our application running instead of this exception ?

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
ssuman
  • 1
  • 1
  • Can't you check for `y` parameter before calling the function? – George May 14 '12 at 06:06
  • 1
    Your question title is misleading, because you ask about handling an exception, but your example is not required to throw one. – juanchopanza May 14 '12 at 06:10
  • possible duplicate of [C++ : Catch a divide by zero error](http://stackoverflow.com/questions/4747934/c-catch-a-divide-by-zero-error) – Yakov Galka May 14 '12 at 06:51
  • Called function is actually a business logic in which I am not allowed to do any modifiaction. Through calling function only I should do something.One way is to call the function in some other thread and check the status. But dont know exactly the procedure.. – ssuman May 14 '12 at 07:34

4 Answers4

3

There are two ways to handle this:

Enforce Parameter checking:

In the calling code you should check the divisor.

if(y != 0)
    func(x,y);
else
  //Some log or error handling

OR

Throw an Exception:

An Integer divided by zero is not an standard C++ exception. So You just cannot rely on an exception being thrown implicitly. An particular compiler might map the divide by zero to some kind of an exception(You will need to check the compiler documentation for this), if so you can catch that particular exception. However, note that this is not portable behavior and will not work for all compilers.

So given the above the best you can do is to check the error condition(divisor equals zero) yourself and throw an exception explicitly say of the type std::runtime_error.


Either case your code should document the behavior appropriately, In first case your code should document valid supported values while in second it should specify what exceptions can be thrown.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • I think OP's example is designed to illustrate a case where you can't check the parameters before calling (without knowing the details of the function's implemnetation). – juanchopanza May 14 '12 at 06:16
  • 1
    @juanchopanza: It is impossible to handle this without *knowing* the implementation detail of the function.And if that be the case the function is badly written enough to deserve an UB and result in a crash. – Alok Save May 14 '12 at 06:18
  • Exactly. But the question is, what to do in these cases? And I think the only viable option is your second suggestion, assuming one cannot replace the function with a well behaved counterpart. – juanchopanza May 14 '12 at 06:21
  • @juanchopanza: Indeed,the second option is only the viable option then. For me re-factoring badly written code is a constantly ongoing process and all such code should be phased out over time.There is an excuse for writing bad code *unknowingly* but there is no excuse for allowing badly written code to run,*knowing* it is bad. – Alok Save May 14 '12 at 06:25
  • I agree completely. Too bad that badly written code often comes without good unit test coverage. – juanchopanza May 14 '12 at 06:26
1

Under Linux you can install a signal handler function for SIGFPE (with signal(2)) and within it throw an exception, and then compile your code with -fnon-call-exceptions.

Once that is done a divide by zero will cause an exception to be thrown.

For Windows there is some stuff mentioned here: Dealing with Floating Point exceptions

Community
  • 1
  • 1
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
0

In general you can:

  1. Check the parameters before calling the function to make sure they are valid. The function should include some documentation that specifies valid ranges of input.
  2. Catch the exception. (This is not applicable in this case, but in general it is an option).
Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
0

You can do this in the calling function:-

if(i == 0)
  cout<<" info::i is equal to zero";
else
  func(5,i);
Abhineet
  • 6,459
  • 10
  • 35
  • 53