I want to check under what circumstance will overflow_error and underflow_error exception be generated in c++, so I wrote a demo to try to generate overflow_error exception, but failed:
#include <iostream>
#include <stdexcept>
using std::cout; using std::endl;
int main() {
try {
int a = std::pow(2, 31) - 1;
cout << a << endl;
int b = 1;
int c = a + b;
cout << c << endl;
} catch (std::overflow_error ex) {
cout << ex.what() << endl;
}
return 0;
}
The out put is:
2147483647
-2147483648
As you can see, no overflow_error exception generated, so, my questions are what the arithmetic overflow errors and arithmetic underflow errors are and how to generate overflow_error and underflow_error exception in c++?