There are two orthogonal aspects to error handling:
Available Actions When an Error is RaisedWhat to do with the error is encapsulated by an enumerated type: namespace boost { namespace math { namespace policies { enum error_policy_type { throw_on_error = 0, // throw an exception. errno_on_error = 1, // set ::errno & return 0, NaN, infinity or best guess. ignore_error = 2, // return 0, NaN, infinity or best guess. user_error = 3 // call a user-defined error handler. }; }}} // namespaces The various enumerated values have the following meanings: throw_on_errorWill throw one of the following exceptions, depending upon the type of the error:
errno_on_error
Will set global
ignore_error
Will return one of the values below depending on the error type (
user_errorWill call a user defined error handler: these are forward declared in boost/math/policies/error_handling.hpp, but the actual definitions must be provided by the user: namespace boost{ namespace math{ namespace policies{ template <class T> T user_domain_error(const char* function, const char* message, const T& val); template <class T> T user_pole_error(const char* function, const char* message, const T& val); template <class T> T user_overflow_error(const char* function, const char* message, const T& val); template <class T> T user_underflow_error(const char* function, const char* message, const T& val); template <class T> T user_denorm_error(const char* function, const char* message, const T& val); template <class T> T user_rounding_error(const char* function, const char* message, const T& val); template <class T> T user_evaluation_error(const char* function, const char* message, const T& val); template <class T> T user_indeterminate_result_error(const char* function, const char* message, const T& val); }}} // namespaces Note that the strings function and message may contain "%1%" format specifiers designed to be used in conjunction with Boost.Format. If these strings are to be presented to the program's end-user then the "%1%" format specifier should be replaced with the name of type T in the function string, and if there is a %1% specifier in the message string then it should be replaced with the value of val. There is more information on user-defined error handlers in the tutorial here. Kinds of Error RaisedThere are six kinds of error reported by this library, which are summarised in the following table:
Examples
Suppose we want a call to
#include <boost/math/special_functions/gamma.hpp> using namespace boost::math::policies; using namespace boost::math; // Define a policy: typedef policy< domain_error<errno_on_error>, pole_error<errno_on_error>, overflow_error<errno_on_error>, policies::evaluation_error<errno_on_error> > my_policy; // call the function: double t1 = tgamma(some_value, my_policy()); // Alternatively we could use make_policy and define everything at the call site: double t2 = tgamma(some_value, make_policy( domain_error<errno_on_error>(), pole_error<errno_on_error>(), overflow_error<errno_on_error>(), policies::evaluation_error<errno_on_error>() ));
Suppose we want a statistical distribution to return infinities, rather than throw exceptions, then we can use:
#include <boost/math/distributions/normal.hpp> using namespace boost::math::policies; using namespace boost::math; // Define a policy: typedef policy< overflow_error<ignore_error> > my_policy; // Define the distribution: typedef normal_distribution<double, my_policy> my_norm; // Get a quantile: double q = quantile(my_norm(), 0.05);
|