]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Error Handling Example</title> | |
5 | <link rel="stylesheet" href="../../../math.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.77.1"> | |
7 | <link rel="home" href="../../../index.html" title="Math Toolkit 2.5.1"> | |
8 | <link rel="up" href="../weg.html" title="Worked Examples"> | |
9 | <link rel="prev" href="nccs_eg/nccs_power_eg.html" title="Tables of the power function of the chi2 test."> | |
10 | <link rel="next" href="find_eg.html" title="Find Location and Scale Examples"> | |
11 | </head> | |
12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> | |
13 | <table cellpadding="2" width="100%"><tr> | |
14 | <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> | |
15 | <td align="center"><a href="../../../../../../../index.html">Home</a></td> | |
16 | <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> | |
17 | <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> | |
18 | <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> | |
19 | <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> | |
20 | </tr></table> | |
21 | <hr> | |
22 | <div class="spirit-nav"> | |
23 | <a accesskey="p" href="nccs_eg/nccs_power_eg.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../weg.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="find_eg.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> | |
24 | </div> | |
25 | <div class="section"> | |
26 | <div class="titlepage"><div><div><h4 class="title"> | |
27 | <a name="math_toolkit.stat_tut.weg.error_eg"></a><a class="link" href="error_eg.html" title="Error Handling Example">Error Handling | |
28 | Example</a> | |
29 | </h4></div></div></div> | |
30 | <p> | |
31 | See <a class="link" href="../../error_handling.html" title="Error Handling">error handling documentation</a> | |
32 | for a detailed explanation of the mechanism of handling errors, including | |
33 | the common "bad" arguments to distributions and functions, and | |
34 | how to use <a class="link" href="../../../policy.html" title="Chapter 15. Policies: Controlling Precision, Error Handling etc">Policies</a> to control it. | |
35 | </p> | |
36 | <p> | |
37 | But, by default, <span class="bold"><strong>exceptions will be raised</strong></span>, | |
38 | for domain errors, pole errors, numeric overflow, and internal evaluation | |
39 | errors. To avoid the exceptions from getting thrown and instead get an | |
40 | appropriate value returned, usually a NaN (domain errors pole errors or | |
41 | internal errors), or infinity (from overflow), you need to change the policy. | |
42 | </p> | |
43 | <p> | |
44 | The following example demonstrates the effect of setting the macro BOOST_MATH_DOMAIN_ERROR_POLICY | |
45 | when an invalid argument is encountered. For the purposes of this example, | |
46 | we'll pass a negative degrees of freedom parameter to the student's t distribution. | |
47 | </p> | |
48 | <p> | |
49 | Since we know that this is a single file program we could just add: | |
50 | </p> | |
51 | <pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_MATH_DOMAIN_ERROR_POLICY</span> <span class="identifier">ignore_error</span> | |
52 | </pre> | |
53 | <p> | |
54 | to the top of the source file to change the default policy to one that | |
55 | simply returns a NaN when a domain error occurs. Alternatively we could | |
56 | use: | |
57 | </p> | |
58 | <pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_MATH_DOMAIN_ERROR_POLICY</span> <span class="identifier">errno_on_error</span> | |
59 | </pre> | |
60 | <p> | |
61 | To ensure the <code class="computeroutput"><span class="special">::</span><span class="identifier">errno</span></code> | |
62 | is set when a domain error occurs as well as returning a NaN. | |
63 | </p> | |
64 | <p> | |
65 | This is safe provided the program consists of a single translation unit | |
66 | <span class="emphasis"><em>and</em></span> we place the define <span class="emphasis"><em>before</em></span> | |
67 | any #includes. Note that should we add the define after the includes then | |
68 | it will have no effect! A warning such as: | |
69 | </p> | |
70 | <pre class="programlisting">warning C4005: 'BOOST_MATH_OVERFLOW_ERROR_POLICY' : macro redefinition</pre> | |
71 | <p> | |
72 | is a certain sign that it will <span class="emphasis"><em>not</em></span> have the desired | |
73 | effect. | |
74 | </p> | |
75 | <p> | |
76 | We'll begin our sample program with the needed includes: | |
77 | </p> | |
78 | <pre class="programlisting"> <span class="preprocessor">#define</span> <span class="identifier">BOOST_MATH_DOMAIN_ERROR_POLICY</span> <span class="identifier">ignore_error</span> | |
79 | ||
80 | <span class="comment">// Boost</span> | |
81 | <span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">distributions</span><span class="special">/</span><span class="identifier">students_t</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> | |
82 | <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">students_t</span><span class="special">;</span> <span class="comment">// Probability of students_t(df, t).</span> | |
83 | ||
84 | <span class="comment">// std</span> | |
85 | <span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> | |
86 | <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">;</span> | |
87 | <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> | |
88 | ||
89 | <span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">stdexcept</span><span class="special">></span> | |
90 | ||
91 | ||
92 | <span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstddef</span><span class="special">></span> | |
93 | <span class="comment">// using ::errno</span> | |
94 | </pre> | |
95 | <p> | |
96 | Next we'll define the program's main() to call the student's t distribution | |
97 | with an invalid degrees of freedom parameter, the program is set up to | |
98 | handle either an exception or a NaN: | |
99 | </p> | |
100 | <pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> | |
101 | <span class="special">{</span> | |
102 | <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Example error handling using Student's t function. "</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> | |
103 | <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"BOOST_MATH_DOMAIN_ERROR_POLICY is set to: "</span> | |
104 | <span class="special"><<</span> <span class="identifier">BOOST_STRINGIZE</span><span class="special">(</span><span class="identifier">BOOST_MATH_DOMAIN_ERROR_POLICY</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> | |
105 | ||
106 | <span class="keyword">double</span> <span class="identifier">degrees_of_freedom</span> <span class="special">=</span> <span class="special">-</span><span class="number">1</span><span class="special">;</span> <span class="comment">// A bad argument!</span> | |
107 | <span class="keyword">double</span> <span class="identifier">t</span> <span class="special">=</span> <span class="number">10</span><span class="special">;</span> | |
108 | ||
109 | <span class="keyword">try</span> | |
110 | <span class="special">{</span> | |
111 | <span class="identifier">errno</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="comment">// Clear/reset.</span> | |
112 | <span class="identifier">students_t</span> <span class="identifier">dist</span><span class="special">(</span><span class="identifier">degrees_of_freedom</span><span class="special">);</span> <span class="comment">// exception is thrown here if enabled.</span> | |
113 | <span class="keyword">double</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">dist</span><span class="special">,</span> <span class="identifier">t</span><span class="special">);</span> | |
114 | <span class="comment">// Test for error reported by other means:</span> | |
115 | <span class="keyword">if</span><span class="special">((</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">isnan</span><span class="special">)(</span><span class="identifier">p</span><span class="special">))</span> | |
116 | <span class="special">{</span> | |
117 | <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"cdf returned a NaN!"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> | |
118 | <span class="keyword">if</span> <span class="special">(</span><span class="identifier">errno</span> <span class="special">!=</span> <span class="number">0</span><span class="special">)</span> | |
119 | <span class="special">{</span> <span class="comment">// So errno has been set.</span> | |
120 | <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"errno is set to: "</span> <span class="special"><<</span> <span class="identifier">errno</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> | |
121 | <span class="special">}</span> | |
122 | <span class="special">}</span> | |
123 | <span class="keyword">else</span> | |
124 | <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Probability of Student's t is "</span> <span class="special"><<</span> <span class="identifier">p</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> | |
125 | <span class="special">}</span> | |
126 | <span class="keyword">catch</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span><span class="special">&</span> <span class="identifier">e</span><span class="special">)</span> | |
127 | <span class="special">{</span> | |
128 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> | |
129 | <span class="string">"\n"</span><span class="string">"Message from thrown exception was:\n "</span> <span class="special"><<</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> | |
130 | <span class="special">}</span> | |
131 | <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> | |
132 | <span class="special">}</span> <span class="comment">// int main()</span> | |
133 | </pre> | |
134 | <p> | |
135 | Here's what the program output looks like with a default build (one that | |
136 | <span class="bold"><strong>does throw exceptions</strong></span>): | |
137 | </p> | |
138 | <pre class="programlisting">Example error handling using Student's t function. | |
139 | BOOST_MATH_DOMAIN_ERROR_POLICY is set to: throw_on_error | |
140 | ||
141 | Message from thrown exception was: | |
142 | Error in function boost::math::students_t_distribution<double>::students_t_distribution: | |
143 | Degrees of freedom argument is -1, but must be > 0 ! | |
144 | </pre> | |
145 | <p> | |
146 | Alternatively let's build with: | |
147 | </p> | |
148 | <pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_MATH_DOMAIN_ERROR_POLICY</span> <span class="identifier">ignore_error</span> | |
149 | </pre> | |
150 | <p> | |
151 | Now the program output is: | |
152 | </p> | |
153 | <pre class="programlisting">Example error handling using Student's t function. | |
154 | BOOST_MATH_DOMAIN_ERROR_POLICY is set to: ignore_error | |
155 | cdf returned a NaN! | |
156 | </pre> | |
157 | <p> | |
158 | And finally let's build with: | |
159 | </p> | |
160 | <pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_MATH_DOMAIN_ERROR_POLICY</span> <span class="identifier">errno_on_error</span> | |
161 | </pre> | |
162 | <p> | |
163 | Which gives the output show errno: | |
164 | </p> | |
165 | <pre class="programlisting">Example error handling using Student's t function. | |
166 | BOOST_MATH_DOMAIN_ERROR_POLICY is set to: errno_on_error | |
167 | cdf returned a NaN! | |
168 | errno is set to: 33 | |
169 | </pre> | |
170 | <div class="caution"><table border="0" summary="Caution"> | |
171 | <tr> | |
172 | <td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../../doc/src/images/caution.png"></td> | |
173 | <th align="left">Caution</th> | |
174 | </tr> | |
175 | <tr><td align="left" valign="top"> | |
176 | <p> | |
177 | If throwing of exceptions is enabled (the default) but you do <span class="bold"><strong>not</strong></span> have try & catch block, then the program | |
178 | will terminate with an uncaught exception and probably abort. | |
179 | </p> | |
180 | <p> | |
181 | Therefore to get the benefit of helpful error messages, enabling <span class="bold"><strong>all exceptions and using try & catch</strong></span> is recommended | |
182 | for most applications. | |
183 | </p> | |
184 | <p> | |
185 | However, for simplicity, the is not done for most examples. | |
186 | </p> | |
187 | </td></tr> | |
188 | </table></div> | |
189 | </div> | |
190 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
191 | <td align="left"></td> | |
192 | <td align="right"><div class="copyright-footer">Copyright © 2006-2010, 2012-2014 Nikhar Agrawal, | |
193 | Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert | |
194 | Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan Råde, Gautam Sewani, | |
195 | Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang<p> | |
196 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
197 | file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) | |
198 | </p> | |
199 | </div></td> | |
200 | </tr></table> | |
201 | <hr> | |
202 | <div class="spirit-nav"> | |
203 | <a accesskey="p" href="nccs_eg/nccs_power_eg.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../weg.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="find_eg.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> | |
204 | </div> | |
205 | </body> | |
206 | </html> |