1 // example_error_handling.cpp
3 // Copyright Paul A. Bristow 2007, 2010.
4 // Copyright John Maddock 2007.
6 // Use, modification and distribution are subject to the
7 // Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt
9 // or copy at http://www.boost.org/LICENSE_1_0.txt)
11 // Note that this file contains quickbook markup as well as code
12 // and comments, don't change any of the special comment markups!
14 // Optional macro definitions described in text below:
15 // #define BOOST_MATH_DOMAIN_ERROR_POLICY ignore_error
16 // #define BOOST_MATH_DOMAIN_ERROR_POLICY errno_on_error
17 // #define BOOST_MATH_DOMAIN_ERROR_POLICY is set to: throw_on_error
19 //[error_handling_example
21 The following example demonstrates the effect of
22 setting the macro BOOST_MATH_DOMAIN_ERROR_POLICY
23 when an invalid argument is encountered. For the
24 purposes of this example, we'll pass a negative
25 degrees of freedom parameter to the student's t
28 Since we know that this is a single file program we could
31 #define BOOST_MATH_DOMAIN_ERROR_POLICY ignore_error
33 to the top of the source file to change the default policy
34 to one that simply returns a NaN when a domain error occurs.
35 Alternatively we could use:
37 #define BOOST_MATH_DOMAIN_ERROR_POLICY errno_on_error
39 To ensure the `::errno` is set when a domain error occurs
40 as well as returning a NaN.
42 This is safe provided the program consists of a single
43 translation unit /and/ we place the define /before/ any
44 #includes. Note that should we add the define after the includes
45 then it will have no effect! A warning such as:
47 [pre warning C4005: 'BOOST_MATH_OVERFLOW_ERROR_POLICY' : macro redefinition]
49 is a certain sign that it will /not/ have the desired effect.
51 We'll begin our sample program with the needed includes:
55 #define BOOST_MATH_DOMAIN_ERROR_POLICY ignore_error
58 #include <boost/math/distributions/students_t.hpp>
59 using boost::math::students_t
; // Probability of students_t(df, t).
73 Next we'll define the program's main() to call the student's t
74 distribution with an invalid degrees of freedom parameter,
75 the program is set up to handle either an exception or a NaN:
80 cout
<< "Example error handling using Student's t function. " << endl
;
81 cout
<< "BOOST_MATH_DOMAIN_ERROR_POLICY is set to: "
82 << BOOST_STRINGIZE(BOOST_MATH_DOMAIN_ERROR_POLICY
) << endl
;
84 double degrees_of_freedom
= -1; // A bad argument!
89 errno
= 0; // Clear/reset.
90 students_t
dist(degrees_of_freedom
); // exception is thrown here if enabled.
91 double p
= cdf(dist
, t
);
92 // Test for error reported by other means:
93 if((boost::math::isnan
)(p
))
95 cout
<< "cdf returned a NaN!" << endl
;
97 { // So errno has been set.
98 cout
<< "errno is set to: " << errno
<< endl
;
102 cout
<< "Probability of Student's t is " << p
<< endl
;
104 catch(const std::exception
& e
)
107 "\n""Message from thrown exception was:\n " << e
.what() << std::endl
;
114 Here's what the program output looks like with a default build
115 (one that *does throw exceptions*):
118 Example error handling using Student's t function.
119 BOOST_MATH_DOMAIN_ERROR_POLICY is set to: throw_on_error
121 Message from thrown exception was:
122 Error in function boost::math::students_t_distribution<double>::students_t_distribution:
123 Degrees of freedom argument is -1, but must be > 0 !
126 Alternatively let's build with:
128 #define BOOST_MATH_DOMAIN_ERROR_POLICY ignore_error
130 Now the program output is:
133 Example error handling using Student's t function.
134 BOOST_MATH_DOMAIN_ERROR_POLICY is set to: ignore_error
138 And finally let's build with:
140 #define BOOST_MATH_DOMAIN_ERROR_POLICY errno_on_error
142 Which gives the output show errno:
145 Example error handling using Student's t function.
146 BOOST_MATH_DOMAIN_ERROR_POLICY is set to: errno_on_error
153 //] [error_handling_eg end quickbook markup]