exception.xml 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
  3. "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
  4. <section id="safe_numerics.exception">
  5. <title>exception</title>
  6. <?dbhtml stop-chunking?>
  7. <section>
  8. <title>Description</title>
  9. <para>Here we describe the data types used to refer to exceptional
  10. conditions which might occur. Note that when we use the word "exception",
  11. we don't mean the C++ term which refers to a data type, but rather the
  12. colloquial sense of a anomaly, irregularity, deviation, special case,
  13. isolated example, peculiarity, abnormality, oddity; misfit, aberration or
  14. out of the ordinary occurrence. This concept of "exception" is more
  15. complex that one would think and hence is not manifested by a single
  16. simple type. A small number of types work together to implement this
  17. concept within the library.</para>
  18. <para>We've leveraged on the <ulink
  19. url="http://en.cppreference.com/w/cpp/error/error_code">std::error_code</ulink>
  20. which is part of the standard library. We don't use all the facilities
  21. that it offers so it's not an exact match, but it's useful and works for
  22. our purposes.</para>
  23. </section>
  24. <section id="safe_numerics.safe_numerics_error">
  25. <title>enum class safe_numerics_error</title>
  26. <para>The following values are those which a numeric result might return.
  27. They resemble the standard error codes used by C++ standard exceptions.
  28. This resemblance is coincidental and they are wholly unrelated to any
  29. codes of similar names. The reason for the resemblance is that the library
  30. started it's development using the standard library codes. But as
  31. development progressed it became clear that the original codes weren't
  32. sufficient so now they stand on their own. Here are a list of error codes.
  33. The description of what they mean is</para>
  34. <para><informaltable>
  35. <tgroup cols="2">
  36. <colspec align="left" colwidth="1*"/>
  37. <colspec align="left" colwidth="3*"/>
  38. <thead>
  39. <row>
  40. <entry align="left">Symbol</entry>
  41. <entry align="left">Description</entry>
  42. </row>
  43. </thead>
  44. <tbody>
  45. <row>
  46. <entry><code>success</code></entry>
  47. <entry>successful operation - no error returned</entry>
  48. </row>
  49. <row>
  50. <entry><code>positive_overflow_error</code></entry>
  51. <entry>A positive number is too large to be represented by the
  52. data type</entry>
  53. </row>
  54. <row>
  55. <entry><code>negative_overflow_error</code></entry>
  56. <entry>The absolute value of a negative number is too large to
  57. be represented by the data type.</entry>
  58. </row>
  59. <row>
  60. <entry><code>domain_error</code></entry>
  61. <entry>the result of an operation is outside the legal range of
  62. the result.</entry>
  63. </row>
  64. <row>
  65. <entry><code>range_error</code></entry>
  66. <entry>an argument to a function or operator is outside the
  67. legal range - e.g. sqrt(-1).</entry>
  68. </row>
  69. <row>
  70. <entry><code>precision_overflow_error</code></entry>
  71. <entry>precision was lost in the course of executing the
  72. operation.</entry>
  73. </row>
  74. <row>
  75. <entry><code>underflow_error</code></entry>
  76. <entry>A number is too close to zero to be represented by the
  77. data type.</entry>
  78. </row>
  79. <row>
  80. <entry><code>uninitialized_value</code></entry>
  81. <entry>According to the C++ standard, the result may be defined
  82. by the application. e.g. 16 &gt;&gt; 10 will result the expected
  83. result of 0 on most machines.</entry>
  84. </row>
  85. </tbody>
  86. </tgroup>
  87. </informaltable></para>
  88. <para>The above listed codes can be transformed to a instance of type
  89. <ulink
  90. url="http://en.cppreference.com/w/cpp/error/error_code"><code>std::error_code</code></ulink>
  91. with the function:</para>
  92. <para><programlisting>std::error_code make_error_code(safe_numerics_error e)</programlisting></para>
  93. <para>This object can be</para>
  94. </section>
  95. <section>
  96. <title>enum class safe_numerics_actions</title>
  97. <para>The above error codes are classified into groups according to how
  98. such exceptions should be handled. The following table shows the possible
  99. actions that an error could be mapped to.</para>
  100. <informaltable>
  101. <tgroup cols="2">
  102. <colspec align="left" colwidth="1*"/>
  103. <colspec align="left" colwidth="3*"/>
  104. <thead>
  105. <row>
  106. <entry align="left">Symbol</entry>
  107. <entry align="left">Description</entry>
  108. </row>
  109. </thead>
  110. <tbody>
  111. <row>
  112. <entry><code>no_action</code></entry>
  113. <entry>successful operation - no action action required</entry>
  114. </row>
  115. <row>
  116. <entry><code>uninitialized_value</code></entry>
  117. <entry>report attempt to use an uninitialized value - not
  118. currently used</entry>
  119. </row>
  120. <row>
  121. <entry><code>arithmetic_error</code></entry>
  122. <entry>report an arithmetic error</entry>
  123. </row>
  124. <row>
  125. <entry><code>implementation_defined_behavior</code></entry>
  126. <entry>report an operation which the C++ standard permits but
  127. fails to specify</entry>
  128. </row>
  129. <row>
  130. <entry><code>undefined_behavior</code></entry>
  131. <entry>report an operation whose result is undefined by the C++
  132. standard.</entry>
  133. </row>
  134. </tbody>
  135. </tgroup>
  136. </informaltable>
  137. <para>Translation of a <code>safe_numerics_error</code> into the
  138. corresponding <code>safe_numerics_action</code> can be accomplished with
  139. the following function:</para>
  140. <para><programlisting>constexpr enum safe_numerics_actions
  141. make_safe_numerics_action(const safe_numerics_error &amp; e);</programlisting></para>
  142. </section>
  143. <section>
  144. <title>See Also</title>
  145. <itemizedlist>
  146. <listitem>
  147. <para><ulink url="http://en.cppreference.com/w/cpp/error">C++ Standard
  148. Library version</ulink> The C++ standard error handling
  149. utilities.</para>
  150. </listitem>
  151. <listitem>
  152. <para><ulink
  153. url="http://blog.think-async.com/2010/04/system-error-support-in-c0x-part-1.html">Thinking
  154. Asynchronously in C++</ulink> Another essential reference on the
  155. design and usage of the error_code</para>
  156. </listitem>
  157. </itemizedlist>
  158. </section>
  159. <section>
  160. <title>Header</title>
  161. <para><ulink
  162. url="../../include/boost/safe_numerics/exception.hpp"><code>#include
  163. &lt;boost/numeric/safe_numerics/exception.hpp&gt;</code></ulink></para>
  164. </section>
  165. </section>