<!-- Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc. -->
<!-- Distributed under 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) -->
<h2>Introduction</h2>
<p>The purpose of Boost Exception is to ease the design of exception class hierarchies and to help write exception handling and error reporting code.</p>
<p>It supports transporting of arbitrary data to the catch site, which is otherwise tricky due to the no-throw requirements (15.5.1) for exception types. Data can be added to any exception object, either directly in the throw-expression (15.1), or at a later time as the exception object propagates up the call stack.</p>
<p>The ability to add data to exception objects after they have been passed to throw is important, because often some of the information needed to handle an exception is unavailable in the context where the failure is detected. </p>
<p>Boost Exception also supports <spanclass="RenoLink"><ahref="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2179.html">N2179</a></span>-style <spanclass="RenoLink"><ahref="tutorial_exception_ptr.html">copying</a></span> of exception objects, implemented non-intrusively and automatically by the boost::<spanclass="RenoLink"><ahref="throw_exception.html">throw_exception</a></span> function.</p>
<li>Tutorial<div><ol><li><spanclass="RenoLink"><ahref="tutorial_transporting_data.html">Transporting of Arbitrary Data to the Catch Site</a></span></li>
<li><spanclass="RenoLink"><ahref="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2179.html">N2179</a></span> Transporting of Exceptions between Threads<div><ol><li><spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span></li>
<spanclass="RenoLink"><ahref="exception_constructors.html">exception</a></span>( <spanclass="RenoLink"><ahref="exception.html">exception</a></span> const & x );</span>
<spanclass="RenoIncludeSPAN"><spanclass="RenoLink"><ahref="error_info_error_info.html">error_info</a></span>( <spanclass="RenoLink"><ahref="error_info_value_type.html">value_type</a></span> const & v );</span>
E const &<spanclass="RenoLink"><ahref="exception_operator_shl.html">operator<<</a></span>( E const & x, <spanclass="RenoLink"><ahref="error_info.html">error_info</a></span><Tag,T> const & v );</span></span>
<spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span><spanclass="RenoLink"><ahref="copy_exception.html">copy_exception</a></span>( T const & e );</span>
<spanclass="RenoIncludeSPAN">void <spanclass="RenoLink"><ahref="rethrow_exception.html">rethrow_exception</a></span>( <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span> const & ep );</span></span>
<spanclass="RenoLink"><ahref="exception_constructors.html">exception</a></span>( <spanclass="RenoLink"><ahref="exception.html">exception</a></span> const & x );</span>
</div><p>Class boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span> is designed to be used as a universal base for user-defined exception types.</p>
<p>An object of any type deriving from boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span> can store data of arbitrary types, using the <spanclass="RenoLink"><ahref="error_info.html">error_info</a></span> wrapper and <spanclass="RenoLink"><ahref="exception_operator_shl.html">operator<<</a></span>.</p>
<p>To retrieve data from a boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span> object, use the <spanclass="RenoLink"><ahref="get_error_info.html">get_error_info</a></span> function template.</p>
<spanclass="RenoIncludeSPAN"><spanclass="RenoLink"><ahref="error_info_error_info.html">error_info</a></span>( <spanclass="RenoLink"><ahref="error_info_value_type.html">value_type</a></span> const & v );</span>
<p>This class template is used to associate a Tag type with a value type T. Objects of type <spanclass="RenoLink"><ahref="error_info.html">error_info</a></span><Tag,T> can be passed to <spanclass="RenoLink"><ahref="exception_operator_shl.html">operator<<</a></span> to be stored in objects of type boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span>.</p>
<p>The header <<spanclass="RenoLink"><ahref="exception_error_info_value_hpp.html">boost/exception/error_info.hpp</a></span>> provides a declaration of the <spanclass="RenoLink"><ahref="error_info.html">error_info</a></span> template, which is sufficient for the purpose of typedefing an instance for specific Tag and T, for example:</p>
<p>This errno_info typedef can be passed to <spanclass="RenoLink"><ahref="exception_operator_shl.html">operator<<</a></span> (#include <<spanclass="RenoLink"><ahref="exception_error_info_hpp.html">boost/exception/info.hpp</a></span>> first) to store an int named tag_errno in exceptions of types that derive from boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span>:</p>
<p>It can also be passed to <spanclass="RenoLink"><ahref="get_error_info.html">get_error_info</a></span> (#include <<spanclass="RenoLink"><ahref="exception_get_error_info_hpp.html">boost/exception/get_error_info.hpp</a></span>> first) to retrieve the tag_errno int from a boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span>:</p>
<pre>catch( boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span>& x )
E const &<spanclass="RenoLink"><ahref="exception_operator_shl.html">operator<<</a></span>( E const & x, <spanclass="RenoLink"><ahref="error_info.html">error_info</a></span><Tag,T> const & v );</span>
<p>E must be boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span>, or a type that derives (indirectly) from boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span>.</p>
<p>Stores a copy of v into x. If x already contains data of type <spanclass="RenoLink"><ahref="error_info.html">error_info</a></span><Tag,T>, that data is overwritten.</p>
<spanclass="RenoLink"><ahref="error_info.html">error_info</a></span><TagN,TN>> const & v );</span>
}</pre>
</div><h4>Requirements:</h4>
<p>E must be boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span>, or a type that derives (indirectly) from boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span>.</p>
<h4>Effects:</h4>
<p>Equivalent to x << v.<spanclass="RenoLink"><ahref="http://www.boost.org/libs/tuple/doc/tuple_users_guide.html#accessing_elements">get</a></span><0>() << ... << v.<spanclass="RenoLink"><ahref="http://www.boost.org/libs/tuple/doc/tuple_users_guide.html#accessing_elements">get</a></span><N>().</p>
<h4>Returns:</h4>
<p>x.</p>
<divclass="RenoIncludeDIV"><h4>Throws:</h4>
<p>std::bad_alloc, or any exception emitted by T1..TN copy constructor.</p>
<spanclass="RenoLink"><ahref="http://www.boost.org/libs/smart_ptr/shared_ptr.htm">shared_ptr</a></span><typename ErrorInfo::value_type const><spanclass="RenoLink"><ahref="get_error_info.html">get_error_info</a></span>( E const & x );</span>
<div><ul><li> If dynamic_cast<boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span> const *>(&x) is 0, or if x does not store an object of type ErrorInfo, the returned value is an empty shared_ptr.</li>
<li> Otherwise, the returned shared_ptr points to the stored value (use <spanclass="RenoLink"><ahref="exception_operator_shl.html">operator<<</a></span> to store values in exception objects.) The shared_ptr is valid even after x has been destroyed.</li>
<p>The interface of <spanclass="RenoLink"><ahref="get_error_info.html">get_error_info</a></span> may be affected by the build <spanclass="RenoLink"><ahref="configuration_macros.html">configuration macros</a></span>.</p>
<div><ul><li> If T derives from boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span>, the returned object is of type T and is a copy of x.</li>
<li> Otherwise, the returned object is of an unspecified type that derives publicly from both T and boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span>. The T sub-object is initialized from x by the T copy constructor.</li>
</div><p>The <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span> type can be used to refer to a copy of an exception object. It is Default Constructible, Copy Constructible, Assignable and Equality Comparable; <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span>'s operations do not throw.</p>
<p>Two instances of <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span> are equivalent and compare equal if and only if they refer to the same exception.</p>
<p>The default constructor of <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span> produces the null value of the type. The null value is equivalent only to itself.</p>
<div><ul><li> It is legal for multiple threads to hold <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span> references to the same exception object.</li>
<li> It is illegal for multiple threads to modify the same <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span> object concurrently.</li>
<li> While calling <spanclass="RenoLink"><ahref="current_exception.html">current_exception</a></span> makes a copy of the current exception object, it is still possible for the two copies to share internal state. Therefore, in general it is not safe to call <spanclass="RenoLink"><ahref="rethrow_exception.html">rethrow_exception</a></span> concurrently to throw the same exception object into multiple threads.</li>
<p>This function is designed to be used directly in a throw-expression to enable the <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span> support in Boost Exception. For example:</p>
<p>Unless <spanclass="RenoLink"><ahref="enable_current_exception.html">enable_current_exception</a></span> is called at the time an exception object is used in a throw-expression, an attempt to copy it using <spanclass="RenoLink"><ahref="current_exception.html">current_exception</a></span> may return an <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span> which refers to an instance of <spanclass="RenoLink"><ahref="unknown_exception.html">unknown_exception</a></span>. See <spanclass="RenoLink"><ahref="current_exception.html">current_exception</a></span> for details.</p>
<p>Instead of using the throw keyword directly, it is preferable to call boost::<spanclass="RenoLink"><ahref="throw_exception.html">throw_exception</a></span>. This is guaranteed to throw an exception that derives from boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span> and supports the <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span> functionality.</p>
<p>The <spanclass="RenoLink"><ahref="current_exception.html">current_exception</a></span> function must not be called outside of a catch block.</p>
<h4>Returns:</h4>
<div><ul><li> An <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span> that refers to the currently handled exception or a copy of the currently handled exception.</li>
<li> If the function needs to allocate memory and the attempt fails, it returns an <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span> that refers to an instance of std::bad_alloc.</li>
</ul></div>
<h4>Throws:</h4>
<p>Nothing.</p>
<h4>Notes:</h4>
<div><ul><li> It is unspecified whether the return values of two successive calls to <spanclass="RenoLink"><ahref="current_exception.html">current_exception</a></span> refer to the same exception object.</li>
<li> Correct implementation of <spanclass="RenoLink"><ahref="current_exception.html">current_exception</a></span> may require compiler support, unless <spanclass="RenoLink"><ahref="enable_current_exception.html">enable_current_exception</a></span> was used at the time the currently handled exception object was passed to throw. If <spanclass="RenoLink"><ahref="enable_current_exception.html">enable_current_exception</a></span> was not used, and if the compiler does not provide the necessary support, then <spanclass="RenoLink"><ahref="current_exception.html">current_exception</a></span> may return an <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span> that refers to an instance of <spanclass="RenoLink"><ahref="unknown_exception.html">unknown_exception</a></span>. In this case, if the original exception object derives from boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span>, then the boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span> sub-object of the <spanclass="RenoLink"><ahref="unknown_exception.html">unknown_exception</a></span> object is initialized by the boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span> copy constructor.</li>
<spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span><spanclass="RenoLink"><ahref="copy_exception.html">copy_exception</a></span>( T const & e );</span>
<spanclass="RenoIncludeSPAN"> void <spanclass="RenoLink"><ahref="rethrow_exception.html">rethrow_exception</a></span>( <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span> const & ep );</span>
public boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span>
{
---unspecified---
};</span>
}</pre>
</div><p>This type is used by the <spanclass="RenoLink"><ahref="exception_ptr.html">exception_ptr</a></span> support in Boost Exception. Please see <spanclass="RenoLink"><ahref="current_exception.html">current_exception</a></span>.</p>
<p>This function returns a string value that is automatically composed from the string representations of all <spanclass="RenoLink"><ahref="error_info.html">error_info</a></span> objects stored in a boost::<spanclass="RenoLink"><ahref="exception.html">exception</a></span> through <spanclass="RenoLink"><ahref="exception_operator_shl.html">operator<<</a></span>, along with other diagnostic information relevant to the exception.</p>
<p>The string representation of each <spanclass="RenoLink"><ahref="error_info.html">error_info</a></span> object is deduced by a function call that is bound at the time the <spanclass="RenoLink"><ahref="error_info.html">error_info</a></span><Tag,T> template is instantiated. The following overload resolutions are attempted in order:</p>
<div><ol><li>Unqualified call to to_string(x), where x is of type <spanclass="RenoLink"><ahref="error_info.html">error_info</a></span><Tag,T> (the return value is expected to be of type std::string.)</li>
<li>Unqualified call to to_string(x.<spanclass="RenoLink"><ahref="error_info_value.html">value</a></span>()) (the return value is expected to be of type std::string.)</li>
<li>Unqualified call to s << x.<spanclass="RenoLink"><ahref="error_info_value.html">value</a></span>(), where s is a std::ostringstream.</li>
</ol></div>
<p>The first successfully bound function is used at the time <spanclass="RenoLink"><ahref="diagnostic_information.html">diagnostic_information</a></span> is called; if all 3 overload resolutions are unsuccessful, the system is unable to convert the <spanclass="RenoLink"><ahref="error_info.html">error_info</a></span> object to string, and <i>an unspecified stub string value is used without issuing a compile error.</i></p>
<p>this is a possible output from the <spanclass="RenoLink"><ahref="diagnostic_information.html">diagnostic_information</a></span> function, as used in <i>libs/exception/example/example_io.cpp:</i></p>
<pre>libs\exception\example\example_io.cpp(83): Throw in function class boost::shared_ptr<struct _iobuf> __cdecl my_fopen(const char *,const char *)
Dynamic exception type: class boost::exception_detail::clone_impl<class fopen_error>
std::exception::what: example_io error
[struct tag_errno *] = 2, OS says "No such file or directory"
<p>E must derive publicly from std::exception.</p>
<h4>Effects:</h4>
<div><ul><li> If BOOST_NO_EXCEPTIONS is not defined, boost::<spanclass="RenoLink"><ahref="throw_exception.html">throw_exception</a></span>(e) is equivalent to throw boost::<spanclass="RenoLink"><ahref="enable_current_exception.html">enable_current_exception</a></span>(boost::<spanclass="RenoLink"><ahref="enable_error_info.html">enable_error_info</a></span>(e)), unless BOOST_EXCEPTION_DISABLE is defined, in which case boost::<spanclass="RenoLink"><ahref="throw_exception.html">throw_exception</a></span>(e) is equivalent to throw e;</li>
<li> If BOOST_NO_EXCEPTIONS is defined, the function is left undefined, and the user is expected to supply an appropriate definition. Callers of <spanclass="RenoLink"><ahref="throw_exception.html">throw_exception</a></span> are allowed to assume that the function never returns; therefore, if the user-defined <spanclass="RenoLink"><ahref="throw_exception.html">throw_exception</a></span> returns, the behavior is undefined.</li>
</div></div><p>This macro takes an exception object, records BOOST_CURRENT_FUNCTION, __FILE__ and __LINE__ in it, and forwards it to <spanclass="RenoLink"><ahref="throw_exception.html">throw_exception</a></span>. To recover this information at the catch site, use <spanclass="RenoLink"><ahref="get_error_info.html">get_error_info</a></span>; the information is also included in the message returned by <spanclass="RenoLink"><ahref="diagnostic_information.html">diagnostic_information</a></span>.</p>
<p>Peter Dimov has been continuously influencing the design and evolution of Boost Exception. Also thanks to Tobias Schwinger, Tom Brinkman, Pavel Vozenilek and everyone who participated in the review process.</p>
<!-- Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc. -->