For some reason there was a change of behavior between boost 1.78 and 1.79 causing now cpp_int convert_to<double>
conversion throwing exceptions for very big numbers. Sometimes it is overflow_error, sometimes domain_error.
I have not found if it is intentional change or not, but right now as a quick first step I would like just to react on this and preserve the old behavior of our code, which was giving infinity for such numbers, std::numeric_limits<double>::max
.
But I have trouble catching the exception. How can I catch instance of boost::wrapexcept<std::overflow_error>
? Is some special handling required? Following simple code - see behavior comparison boost 78 vs 79 here - is not catching the exception but just terminates with
terminate called after throwing an instance of 'boost::wrapexceptstd::overflow_error'
what(): Error in function float_next(double): Overflow Error
#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
int main() {
std::string strNumber = "179769313486231590617005494896502488139538923424507473845653439431848569886227202866765261632299351819569917639009010788373365912036255753178371299382143631760131695224907130882552454362167933328609537509415576609030163673758148226168953269623548572115351901405836315903312675793605327103910016259918212890625";
boost::multiprecision::cpp_int number(strNumber);
try {
std::cout << number.convert_to<double>() << "\n";
} catch (boost::wrapexcept<std::overflow_error> &e) {
std::cout << "boost exception caught" << "\n";
} catch (...) {
std::cout << "some exception caught" << "\n";
}
std::cout << "finished" << "\n";
}
I am expecting at least "some exception caught" line to be called, but it is not. Thanks for help.
Note after reading some answers:
The code is just an example. My current concern is not to convert the number to double. That is another following issue. More important for me right now is actually to catch the exception somehow so that I can return std::numeric_limits<double>::max()
instead of exception and the existing program will work the same way as with older boost (breaking change concerns).
Note 2: This seems to be a bug in boost, caused by throwing an exception in noexcept method. That terminates the program without possibility to catch the exception. Thanks @Ext3h for help. I reported the thing in boost and will properly answer and close this issue if confirmed.