0

I have a problem with the floating point exception. When i divide by zero, i get this exception. I tried to catch it, but the solutions on the internet don't work for me.

#include<iostream>

using namespace std;

int main(){

double h{0};
int a{0},b{0},c{0};

cin.exceptions(ios_base::failbit);

cout << "Enter Values: ";
try{ 
  cin >> a >> b >> c;  
  h = (3/1/a+1/b+1/c);      

if(a == 0 || b == 0 || c == 0){
 throw overflow_error("Division by zero is not allowed!");
}
  cout << h;
}
catch(overflow_error e){
     cerr << e.what();
 }
 catch(exception& e){
     cerr << "Only numbers!";
 }
 catch(...){
    cerr << "?";
}
return 0;
}
myword
  • 3
  • 3
  • There are many kind of exceptions, not all of them are C++ exceptions. And you can only catch a C++ exception that is thrown with the C++ statement `throw`. – Some programmer dude Apr 01 '19 at 09:09
  • and how excactly i can throw it? – myword Apr 01 '19 at 09:11
  • 1
    The right-hand-side of the `h=` assignment just contains integers, so you are only performing integer-divisions by zero (not floating-point). Integer-Division by zero is undefined behavior (i.e., it could crash or give unexpected results or (less likely) format your hard-drive) – chtz Apr 01 '19 at 09:14
  • this is the scope, h have to be a float – myword Apr 01 '19 at 09:17
  • 2
    You could check [the floating point environment](https://en.cppreference.com/w/cpp/numeric/fenv). And depending on compiler and OS you could look into *structured exceptions*. – Some programmer dude Apr 01 '19 at 09:28
  • i think at the moment this is to high for my level. Can you simpley explain, how to chatch a exception that is not a c++ exception? – myword Apr 01 '19 at 09:34
  • With `catch`? You can't. If you you don't feel comfortable manipulating or checking the FP-environment or with structured exceptions, then your only solution is input validation to make sure you never get e.g. division by zero. – Some programmer dude Apr 01 '19 at 09:35
  • why its not possiple? its the task, so there should be a soulution. The problem is, that i dont exactly understand what you mean. Can you show me a sample? – myword Apr 01 '19 at 09:41
  • Time to read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve]. Most importantly, *what is "the task"?* – Some programmer dude Apr 01 '19 at 09:43
  • what has this to do with my question? I think the question is clear enough. If you don't wanna help you are free to go. I – myword Apr 01 '19 at 09:45
  • No, you cannot catch a floating point exception with `catch` (and why you think you can catch it with `catch(int&)` is a complete mystery). FPE is not a C++ exception. It is undefined behaviour to divide by 0. Your platform may offer platform-specific ways to handle this situation, so if you have an assignment to do that, you should ask your professor which platform(s) you need to target. – n. m. could be an AI Apr 01 '19 at 10:48
  • i have catched it successfully . so... why you think its not possible. Look at me edited code – myword Apr 01 '19 at 10:50
  • You didn't catch a division by zero. You caught an exception you have thrown yourself, which is of course possible. – n. m. could be an AI Apr 01 '19 at 10:54
  • That what i wanted. Too stop the the floating point exception – myword Apr 01 '19 at 10:56

1 Answers1

1

You’re getting a “floating point exception” (which is not a C++ exception), that's why its not catchable.

https://www.quora.com/Why-isn%E2%80%99t-this-catch-block-in-C++-catching-any-exception

I suggest guarding for input validation.

In your code, the exception happens before you start throwing stuff.

Ali Ben Zarrouk
  • 1,891
  • 16
  • 24