Files
exception/doc/cloning.html

99 lines
6.6 KiB
HTML
Raw Normal View History

2008-03-04 01:41:17 +00:00
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<title>Tutorial: Transporting of Exceptions between Threads</title>
<link href='reno.css' type='text/css' rel='stylesheet'/>
</head>
<body>
<div class="body-0">
<div class="body-1">
<div class="body-2">
<div>
<div id="boost_logo">
<a href="http://www.boost.org"><img style="border:0" src="http://www.boost.org/boost.png" alt="Boost" width="277" height="86"/></a>
</div>
<h1>Boost Exception</h1>
</div>
<div class="RenoIncludeDIV"><h2>Transporting of Exceptions between Threads</h2>
<p>Boost Exception supports transporting of exception objects between threads through cloning. This system is similar to <span class="RenoLink"><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2179.html">N2179</a></span>, but because Boost Exception can not rely on language support, the use of <tt><span class="RenoLink"><a href="enable_current_exception.html">enable_current_exception</a></span>()</tt> at the time of the throw is required in order to use cloning.</p>
2008-03-04 01:41:17 +00:00
<h4>Note:</h4>
<p>All exceptions emitted by the familiar function <tt>boost::<span class="RenoLink"><a href="throw_exception.html">throw_exception</a></span>()</tt> are guaranteed to derive from <tt>boost::<span class="RenoLink"><a href="exception.html">exception</a></span></tt> and to support cloning.</p>
<div class="RenoIncludeDIV"><h3>Using enable_current_exception() at the Time of the Throw</h3>
2008-03-04 01:41:17 +00:00
<p>Here is how cloning can be enabled in a throw-expression (15.1):</p>
<pre>#include &lt;<span class="RenoLink"><a href="exception_enable_exception_cloning_hpp.html">boost/exception/enable_current_exception.hpp</a></span>&gt;
2008-03-04 01:41:17 +00:00
#include &lt;<span class="RenoLink"><a href="exception_error_info_hpp.html">boost/exception/info.hpp</a></span>&gt;
#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;
typedef boost::error_info&lt;struct tag_errno,int&gt; errno_info;
class file_read_error: public boost::<span class="RenoLink"><a href="exception.html">exception</a></span> { };
void
file_read( FILE * f, void * buffer, size_t size )
{
if( size!=fread(buffer,1,size,f) )
throw boost::<span class="RenoLink"><a href="enable_current_exception.html">enable_current_exception</a></span>(file_read_error()) &lt;&lt;
2008-03-04 01:41:17 +00:00
errno_info(errno);
}</pre>
</div><div class="RenoIncludeDIV"><h3>Cloning and Re-throwing an Exception</h3>
<p>When you catch a <tt>boost::<span class="RenoLink"><a href="exception.html">exception</a></span></tt>, you can call <tt><span class="RenoLink"><a href="current_exception.html">current_exception</a></span>()</tt> to get an <tt><span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span></tt> object:</p>
<pre>#include &lt;<span class="RenoLink"><a href="exception_cloning_hpp.html">boost/exception_ptr.hpp</a></span>&gt;
2008-03-04 01:41:17 +00:00
#include &lt;boost/thread.hpp&gt;
#include &lt;boost/bind.hpp&gt;
void do_work(); //throws cloning-enabled boost::<span class="RenoLink"><a href="exception.html">exception</a></span>s
void
worker_thread( boost::<span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span> &amp; error )
{
try
{
do_work();
error = boost::<span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span>();
}
catch(
... )
2008-03-04 01:41:17 +00:00
{
error = boost::<span class="RenoLink"><a href="current_exception.html">current_exception</a></span>();
2008-03-04 01:41:17 +00:00
}
}</pre>
<p>In the above example, note that <tt><span class="RenoLink"><a href="current_exception.html">current_exception</a></span>()</tt> captures the original type of the exception object, even though <tt>e</tt> refers to the base type <tt>boost::<span class="RenoLink"><a href="exception.html">exception</a></span></tt>. This original type can be thrown again using the <tt><span class="RenoLink"><a href="rethrow_exception.html">rethrow_exception</a></span>()</tt> function:</p>
2008-03-04 01:41:17 +00:00
<pre>// ...continued
void
work()
{
boost::<span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span> error;
boost::<span class="RenoLink"><a href="http://www.boost.org/doc/html/boost/thread.html">thread</a></span> t( boost::<span class="RenoLink"><a href="http://www.boost.org/libs/bind/bind.html">bind</a></span>(worker_thread,boost::<span class="RenoLink"><a href="http://www.boost.org/doc/html/ref.html">ref</a></span>(error)) );
t.<span class="RenoLink"><a href="http://www.boost.org/doc/html/boost/thread.html">join</a></span>();
if( error )
boost::<span class="RenoLink"><a href="rethrow_exception.html">rethrow_exception</a></span>(error);
}</pre>
<p><tt><span class="RenoLink"><a href="current_exception.html">Clone_exception</a></span>()</tt> could fail to copy the original exception object in the following cases:</p>
2008-03-04 01:41:17 +00:00
<div><ul><li> if there is not enough memory, in which case the returned <tt><span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span></tt> points to an instance of <tt>std::bad_alloc</tt>, or</li>
<li> if <tt><span class="RenoLink"><a href="enable_current_exception.html">enable_current_exception</a></span>()</tt> was not used in the throw-expression passed to the original <tt>throw</tt> statement, in which case the returned <tt><span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span></tt> points to an instance of <tt><span class="RenoLink"><a href="unknown_exception.html">unknown_exception</a></span></tt>.</li>
2008-03-04 01:41:17 +00:00
</ul></div>
<p>Regardless, the use of <tt><span class="RenoLink"><a href="current_exception.html">current_exception</a></span>()</tt> and <tt><span class="RenoLink"><a href="rethrow_exception.html">rethrow_exception</a></span>()</tt> in the above examples is well-formed.</p>
2008-03-04 01:41:17 +00:00
</div></div><h3>See also:</h3>
<div class="RenoPageList"><a href="boost-exception.html">Boost Exception<br/>
</a><a href="unknown_exception.html">unknown_exception<br/>
</a></div>
<div id="footer">
<p>&nbsp;</p>
<hr/>
<p>
<a class="logo" href="http://jigsaw.w3.org/css-validator/validator?uri=http://revergestudios.com/boost-exception/reno.css"><img class="logo_pic" src="valid-css.png" alt="Valid CSS" height="31" width="88"/></a>
<a class="logo" href="http://validator.w3.org/check?uri=referer"><img class="logo_pic" src="valid-xhtml.png" alt="Valid XHTML 1.0" height="31" width="88"/></a>
<small>Copyright (c) 2006-2008 by Emil Dotchevski and Reverge Studios, Inc.<br/>
Distributed under the <a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License, Version 1.0</a>.</small>
</p>
</div>
</div>
</div>
</div>
</body>
</html>