Reputation: 837
I started to learn C++ programming and have a question about error handling.
I've made a code that calculates x from the function ax+b=0
(so I have to divide -b
by a
). The values are entered by users via cin >>
If I divide by 0 i get -int
as my output. Is it possible to catch the error (e.g. in with an if
statement)?
I know that dividing by zero is impossible and I also know that it wouldn't be a good behavior form a program, if it doesn't check the user's input (e.g. if ((a != 0)){calculate}
). The thing is I would like to know how if/how it works to catch this error ;-) Does it depend on the hardware, operating system or compiler?
My teachers couldn't help me ;)
Btw. I use Eclipse Juno IDE for C/C++ on Mac OS X 10.8.2
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
float a, b, x; //float da Kommazahlen erwartet werden
cout << "ax + b = 0" << '\n'<< endl;
cout << "Bitte geben Sie einen Wert für a ein:" << endl;
cin >> a;
cout << "Bitte geben Sie einen Wert für b ein:" << endl;
cin >> b;
x = -b/a;
cout << "Ergebnis:" << x << endl;
if (x == #INF )
{
cout << "Du bist a Volldepp - durch Null kann man nicht teilen!" << endl;
}
return 0;
}
Upvotes: 2
Views: 17199
Reputation: 264391
Yes:
In C++03
if ((x == +std::numeric_limits<float>::infinity()) ||
(x == -std::numeric_limits<float>::infinity())
)
In C++11
if (std::isinf(x))
Upvotes: 7
Reputation: 117
Yes, best way to handle this situation is a==0 condition. Divide by zero is not an exception, it is handled at hardware level. The hardware sends interrupt to OS and OS to your application thus crashing it. It can be captured with signal:
#include <csignal>
#include <iostream>
using namespace std;
void handler(int nVal)
{
cout << "Divid By Zero Error" << endl;
}
int main()
{
signal(SIGFPE, handler);
int nVal = 1/0;
}
Upvotes: 1
Reputation: 7010
I echo the other posters in saying that you should check your arguments, but there is an alternative: http://en.cppreference.com/w/cpp/numeric/math/math_errhandling
According to that link, in C++11, you can set a #define
to make it so that when you divide by zero it will throw exceptions of type FE_DIVBYZERO
. But the documentation on that page is NOT clear, so investigate to see if your own compiler supports this.
Upvotes: 1
Reputation: 363567
Just check whether a == 0
before attempting the division:
if (a == 0) {
std::cerr << "I'm not even going to try\n";
return 1;
} else {
std::cout << "-b/a = " << (-b/a) << std::endl;
}
That may still produce inf
for some very small numbers, though.
(Note that in general, checking whether a float
is equal to some value is not reliable because of round-off errors, but for zero it's ok.)
Upvotes: 4
Reputation: 16253
You should check for correctness of input before you calculate, not afterwards:
if ( a == 0 ) {
if ( b == 0 )
cout << "equation valid for all x" << endl;
else
cout << "no x satisfies this equation" << endl;
}
Upvotes: 1
Reputation: 9126
You can not Catch the Exception in C
but you can avoid it by checking the Value..
Try like this, It will help you
if (a == 0) {
x = 0;
} else {
x = b/a;
}
Upvotes: 0