diff --git a/doc/overview/error_handling.qbk b/doc/overview/error_handling.qbk index 14bc1627e..922a80e0c 100644 --- a/doc/overview/error_handling.qbk +++ b/doc/overview/error_handling.qbk @@ -12,6 +12,10 @@ Handling of errors by this library is split into two orthogonal parts: [warning The default error actions are to throw an exception with an informative error message. [role red If you do not try to catch the exception, you will not see the message!]] +The default behavior is for exceptions to be thrown using boost::throw_exception. +This facility has special handling for exception free environments. +If BOOST_MATH_STANDALONE is defined standard library facilities (e.g. throw) are used for exception handling. + The kinds of errors that can be raised are: [variablelist diff --git a/include/boost/math/cstdfloat/cstdfloat_cmath.hpp b/include/boost/math/cstdfloat/cstdfloat_cmath.hpp index eea3328cc..b71bbd828 100644 --- a/include/boost/math/cstdfloat/cstdfloat_cmath.hpp +++ b/include/boost/math/cstdfloat/cstdfloat_cmath.hpp @@ -24,7 +24,7 @@ #include #include #include -#include +#include #if defined(_WIN32) && defined(__GNUC__) // Several versions of Mingw and probably cygwin too have broken @@ -988,7 +988,7 @@ namespace std v = quadmath_snprintf(&buf2[0], v_max + 3, format.c_str(), digits, m_value); if (v >= v_max + 3) { - BOOST_THROW_EXCEPTION(std::runtime_error("Formatting of float128_type failed.")); + BOOST_MATH_THROW_EXCEPTION(std::runtime_error("Formatting of float128_type failed.")); } s = &buf2[0]; } diff --git a/include/boost/math/cstdfloat/cstdfloat_iostream.hpp b/include/boost/math/cstdfloat/cstdfloat_iostream.hpp index 93dc0c2da..e05b3b86f 100644 --- a/include/boost/math/cstdfloat/cstdfloat_iostream.hpp +++ b/include/boost/math/cstdfloat/cstdfloat_iostream.hpp @@ -29,7 +29,7 @@ #include #include #include - #include + #include // #if (0) #if defined(__GNUC__) @@ -86,7 +86,7 @@ my_digits, x); - if(v < 0) { BOOST_THROW_EXCEPTION(std::runtime_error("Formatting of boost::float128_t failed internally in quadmath_snprintf().")); } + if(v < 0) { BOOST_MATH_THROW_EXCEPTION(std::runtime_error("Formatting of boost::float128_t failed internally in quadmath_snprintf().")); } if(v >= static_cast(sizeof(my_buffer) - 1U)) { @@ -106,7 +106,7 @@ } catch(const std::bad_alloc&) { - BOOST_THROW_EXCEPTION(std::runtime_error("Formatting of boost::float128_t failed while allocating memory.")); + BOOST_MATH_THROW_EXCEPTION(std::runtime_error("Formatting of boost::float128_t failed while allocating memory.")); } #endif const int v2 = ::quadmath_snprintf(my_buffer2, @@ -117,7 +117,7 @@ if(v2 >= v + 3) { - BOOST_THROW_EXCEPTION(std::runtime_error("Formatting of boost::float128_t failed.")); + BOOST_MATH_THROW_EXCEPTION(std::runtime_error("Formatting of boost::float128_t failed.")); } static_cast(ostr << my_buffer2); @@ -152,7 +152,7 @@ is.setstate(ios_base::failbit); - BOOST_THROW_EXCEPTION(std::runtime_error("Unable to interpret input string as a boost::float128_t")); + BOOST_MATH_THROW_EXCEPTION(std::runtime_error("Unable to interpret input string as a boost::float128_t")); } return is; @@ -760,7 +760,7 @@ is.setstate(ios_base::failbit); - BOOST_THROW_EXCEPTION(std::runtime_error("Unable to interpret input string as a boost::float128_t")); + BOOST_MATH_THROW_EXCEPTION(std::runtime_error("Unable to interpret input string as a boost::float128_t")); } return is; diff --git a/include/boost/math/policies/error_handling.hpp b/include/boost/math/policies/error_handling.hpp index 45941e3d1..9b8ac8e9b 100644 --- a/include/boost/math/policies/error_handling.hpp +++ b/include/boost/math/policies/error_handling.hpp @@ -8,20 +8,20 @@ #ifndef BOOST_MATH_POLICY_ERROR_HANDLING_HPP #define BOOST_MATH_POLICY_ERROR_HANDLING_HPP -#include #include #include #include #include #include -#include -#include +#include +#include +#include #include #include #include #include -#include -#include +#include + #ifdef BOOST_MSVC # pragma warning(push) // Quiet warnings in boost/format.hpp # pragma warning(disable: 4996) // _SCL_SECURE_NO_DEPRECATE @@ -140,7 +140,7 @@ void raise_error(const char* pfunction, const char* message) msg += message; E e(msg); - boost::throw_exception(e); + BOOST_MATH_THROW_EXCEPTION(e) } template @@ -167,7 +167,7 @@ void raise_error(const char* pfunction, const char* pmessage, const T& val) msg += message; E e(msg); - boost::throw_exception(e); + BOOST_MATH_THROW_EXCEPTION(e) } template @@ -804,7 +804,7 @@ inline R checked_narrowing_cast(T val, const char* function) BOOST_NOEXCEPT_IF(B } template -inline void check_series_iterations(const char* function, boost::uintmax_t max_iter, const Policy& pol) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T) && is_noexcept_error_policy::value) +inline void check_series_iterations(const char* function, std::uintmax_t max_iter, const Policy& pol) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T) && is_noexcept_error_policy::value) { if(max_iter >= policies::get_max_series_iterations()) raise_evaluation_error( @@ -813,7 +813,7 @@ inline void check_series_iterations(const char* function, boost::uintmax_t max_i } template -inline void check_root_iterations(const char* function, boost::uintmax_t max_iter, const Policy& pol) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T) && is_noexcept_error_policy::value) +inline void check_root_iterations(const char* function, std::uintmax_t max_iter, const Policy& pol) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T) && is_noexcept_error_policy::value) { if(max_iter >= policies::get_max_root_iterations()) raise_evaluation_error( diff --git a/include/boost/math/special_functions/nonfinite_num_facets.hpp b/include/boost/math/special_functions/nonfinite_num_facets.hpp index cd1eba932..03af4216f 100644 --- a/include/boost/math/special_functions/nonfinite_num_facets.hpp +++ b/include/boost/math/special_functions/nonfinite_num_facets.hpp @@ -22,10 +22,7 @@ #include #include #include - -#include -#include - +#include #include #include @@ -104,7 +101,7 @@ namespace boost { case FP_INFINITE: if(flags_ & trap_infinity) { - BOOST_THROW_EXCEPTION(std::ios_base::failure("Infinity")); + BOOST_MATH_THROW_EXCEPTION(std::ios_base::failure("Infinity")); } else if((boost::math::signbit)(val)) { // negative infinity. @@ -123,7 +120,7 @@ namespace boost { case FP_NAN: if(flags_ & trap_nan) { - BOOST_THROW_EXCEPTION(std::ios_base::failure("NaN")); + BOOST_MATH_THROW_EXCEPTION(std::ios_base::failure("NaN")); } else if((boost::math::signbit)(val)) { // negative so "-nan". diff --git a/include/boost/math/special_functions/owens_t.hpp b/include/boost/math/special_functions/owens_t.hpp index e8e9343a6..1f2e270ac 100644 --- a/include/boost/math/special_functions/owens_t.hpp +++ b/include/boost/math/special_functions/owens_t.hpp @@ -17,15 +17,15 @@ #endif #include -#include #include #include -#include +#include #include #include #include #include +#include #ifdef BOOST_MSVC #pragma warning(push) @@ -829,7 +829,7 @@ namespace boost val = owens_t_T6(h,a, pol); break; default: - BOOST_THROW_EXCEPTION(std::logic_error("selection routine in Owen's T function failed")); + BOOST_MATH_THROW_EXCEPTION(std::logic_error("selection routine in Owen's T function failed")); } return val; } diff --git a/include/boost/math/tools/throw_exception.hpp b/include/boost/math/tools/throw_exception.hpp new file mode 100644 index 000000000..a1d2cda90 --- /dev/null +++ b/include/boost/math/tools/throw_exception.hpp @@ -0,0 +1,20 @@ +// (C) Copyright Matt Borland 2021. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_MATH_TOOLS_THROW_EXCEPTION_HPP +#define BOOST_MATH_TOOLS_THROW_EXCEPTION_HPP + +#ifndef BOOST_MATH_STANDALONE + +#include +#define BOOST_MATH_THROW_EXCEPTION(expr) boost::throw_exception(expr); + +#else // Standalone mode - use standard library facilities + +#define BOOST_MATH_THROW_EXCEPTION(expr) throw expr; + +#endif // BOOST_MATH_STANDALONE + +#endif // BOOST_MATH_TOOLS_THROW_EXCEPTION_HPP diff --git a/test/compile_test/sf_prime_incl_test.cpp b/test/compile_test/sf_prime_incl_test.cpp index 6ab758479..647a40624 100644 --- a/test/compile_test/sf_prime_incl_test.cpp +++ b/test/compile_test/sf_prime_incl_test.cpp @@ -12,17 +12,18 @@ // important if this test is to be meaningful: // #include "test_compile_result.hpp" +#include void compile_and_link_test() { - check_result(boost::math::prime(u)); + check_result(boost::math::prime(u)); // // Add constexpr tests here: // #ifdef BOOST_MATH_HAVE_CONSTEXPR_TABLES - constexpr boost::uint32_t ce_f = boost::math::prime(boost::math::max_prime); + constexpr std::uint32_t ce_f = boost::math::prime(boost::math::max_prime); static_assert(ce_f == 104729, "max_prime had incorrect value"); - check_result(ce_f); + check_result(ce_f); #endif }