Exceptions

I've been working with exceptions in Object Programming for C++, and it's been easy thus far, but I've been struggling to understand my first assignment. I'm suppose to simply add a try-block and catch-block to this and make it so the program will work correctly.. I know how to do that, but I don't know what the exception is. When I run the program, after inputting a number, it runs infinitely, but I'm not sure what kind of error that is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
	double number, power;

	cout<<"\nEnter a number: "; cin>>number;
	for(long long k=1;; k+=100)
	{
		power = pow(number,k);
		cout<<"\nResult: "<<number<<"^"<<k<<"="<<power<<endl;
	}

	cout<<endl;
	system("pause");
	return 0;
}
Check the documentation:

If the base is finite negative and the exponent is finite but not an integer value, it causes a domain error.
If both base and exponent are zero, it may also cause a domain error on certain implementations.
If base is zero and exponent is negative, it may cause a domain error or a pole error (or none, depending on the library implementation).
The function may also cause a range error if the result is too great or too small to be represented by a value of the return type.

http://www.cplusplus.com/reference/cmath/pow/?kw=pow

If you're not sure what kind of exception is going to be thrown, you can always use a generic catch block:
1
2
3
catch (...)
{  <statements> 
}
Last edited on
...and none of those errors result in any C++ exceptions being thrown (unless you enable some non-standard hacks mentioned at http://en.cppreference.com/w/cpp/numeric/fenv#Notes )
Last edited on
Topic archived. No new replies allowed.