# Thread: Catching an int overflow error

1. ## Catching an int overflow error

Hey everyone, I've got some code where I'm calculating a geometric mean:

Code:
```include <exception>
using std:: overflow_error  // space to prevent a smiley!
...
try {
// Calculate the geometric mean of the values in array prevBids
int product = 1;
for (int i = 0; i < maxPrevBids; i++) {
product *= prevBids[i];
}
bid = (static_cast<int>( pow (product, ( 1.00 / static_cast<double>(maxPrevBids)))));
} catch (overflow_error &e) {
// Some error-handling code
}```
Now sometimes the value of product gets too big for an integer, and I want to somehow catch this error when it happens. I could change it to a long, but I'd still want to catch the overflow error if it happened with a long, so the type of product doesn't really matter to my question. (I can potentially get all sorts of crazy numbers in this code and I need to be able to deal with them all!)

I tried enclosing the code in a try/catch block like shown above, but when I stepped through the code with the debugger, it didn't throw an exception. It just kept trying to multiply product (resulting in some very weird values for product). If product was an invalid entry for the pow function, then an exception was thrown at the pow function.

Any ideas on how I can catch or prevent this overflow error?

2. That is amazing -- I changed it to a double and I never got that error (at least with the parameters I ran the program with). I guess a double can hold larger numbers than an int?

Still, I'm curious if anyone knows how to catch overflow errors like I mentioned in my first post, because that's something that I'm sure other people may find useful as well!

3. In C++, there is no such thing as an overflow. If you keep looping with the for loop for( int i = 0; ; i++ ); the value of i would eventually become negative.

4. Then what's the overflow_error I found in the exception class in the STL? Is it more like a stack overflow?

5. You can put in your own testing routines:

Code:
```int multiply(int a, int b)
{
if(a > (MAX_INT / b))
throw new my_overflow_error_exception(__LINE__);
}```

6. ## A couple of things for you to look-up...

I guess a double can hold larger numbers than an int?
Check-out sizeof() and the <limits> header file. The C++ standard specifies a minimum "capacity" for each type. The actual size of each variable is system dependent.
In C++, there is no such thing as an overflow. If you keep looping with the for loop for( int i = 0; ; i++ ); the value of i would eventually become negative.
Well, I'd call that an overflow error! (Unless it's what you wanted.) Also an unsigned int won't go negative. It will start-over at zero like an odometer rolling-over.

[EDIT]
Once, I was working on a problem with a system that plotted pressure (psi) on a chart recorder. At a particular pressure, the chart-recorder would go erratic. With the Analog-to-Digital conversion from the pressure sensor, then Digital-to-Analog conversion for the chart recorder, we couldn't see the count that was overflowing in binary and it was very difficult to determine that it was an overflow problem!

7. 19.1.8 class overflow_error [lib.overflow.error]
The class overflow_error defines the type of objects thrown as exceptions to report an arithmetic overflow error.
But there are very few methodes actually throwing overflow_error.
For example
bitset<N>::to_ulong() const;
Throws: overflow_error if the integral value can not represented as type unsigend long

8. thanks everyone... very helpful info