• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
2'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
3<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
4<head>
5	<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
6	<title>transporting of exceptions between threads</title>
7	<link href='reno.css' type='text/css' rel='stylesheet'/>
8</head>
9<body>
10<div class="body-0">
11<div class="body-1">
12<div class="body-2">
13<div>
14<div id="boost_logo">
15<a href="http://www.boost.org"><img style="border:0" src="../../../boost.png" alt="Boost" width="277" height="86"/></a>
16</div>
17<h1>Boost Exception</h1>
18</div>
19<!-- Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc. -->
20<!-- Distributed under the Boost Software License, Version 1.0. (See accompanying -->
21<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
22<div class="RenoIncludeDIV"><div class="RenoAutoDIV"><h2>Transporting of Exceptions Between Threads</h2>
23</div>
24<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 <span class="RenoLink"><a href="enable_current_exception.html">enable_current_exception</a></span> at the time of the throw is required in order to use cloning.</p>
25<h4>Note:</h4>
26<p>All exceptions emitted by the familiar function boost::<span class="RenoLink"><a href="throw_exception.html">throw_exception</a></span> are guaranteed to derive from boost::<span class="RenoLink"><a href="exception.html">exception</a></span> and to support cloning.</p>
27<div class="RenoIncludeDIV"><div class="RenoAutoDIV"><h3>Using enable_current_exception at the Time of the Throw</h3>
28</div>
29<p>Here is how cloning can be enabled in a throw-expression (15.1):</p>
30<pre>#include &lt;<span class="RenoLink"><a href="boost_exception_info_hpp.html">boost/exception/info.hpp</a></span>&gt;
31#include &lt;<span class="RenoLink"><a href="boost_exception_errinfo_errno_hpp.html">boost/exception/errinfo_errno.hpp</a></span>&gt;
32#include &lt;stdio.h&gt;
33#include &lt;errno.h&gt;
34
35struct file_read_error: virtual boost::<span class="RenoLink"><a href="exception.html">exception</a></span> { };
36
37void
38file_read( FILE * f, void * buffer, size_t size )
39    {
40    if( size!=fread(buffer,1,size,f) )
41        throw boost::<span class="RenoLink"><a href="enable_current_exception.html">enable_current_exception</a></span>(file_read_error()) &lt;&lt;
42            boost::<span class="RenoLink"><a href="errinfo_errno.html">errinfo_errno</a></span>(errno);
43    }</pre>
44<p>Of course, <span class="RenoLink"><a href="enable_current_exception.html">enable_current_exception</a></span> may be used with any exception type; there is no requirement that it should derive from boost::<span class="RenoLink"><a href="exception.html">exception</a></span>.</p>
45</div><div class="RenoIncludeDIV"><div class="RenoAutoDIV"><h3>Cloning and Re-Throwing an Exception</h3>
46</div>
47<p>When you catch an exception, you can call <span class="RenoLink"><a href="current_exception.html">current_exception</a></span> to get an <span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span> object:</p>
48<pre>#include &lt;<span class="RenoLink"><a href="boost_exception_ptr_hpp.html">boost/exception_ptr.hpp</a></span>&gt;
49#include &lt;boost/thread.hpp&gt;
50#include &lt;boost/bind.hpp&gt;
51
52void do_work(); //throws cloning-enabled boost::<span class="RenoLink"><a href="exception.html">exception</a></span>s
53
54void
55worker_thread( boost::<span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span> &amp; error )
56    {
57    try
58        {
59        do_work();
60        error = boost::<span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span>();
61        }
62    catch(
63    ... )
64        {
65        error = boost::<span class="RenoLink"><a href="current_exception.html">current_exception</a></span>();
66        }
67    }</pre>
68<p>In the above example, note that <span class="RenoLink"><a href="current_exception.html">current_exception</a></span> captures the original type of the exception object. The exception can be thrown again using the <span class="RenoLink"><a href="rethrow_exception.html">rethrow_exception</a></span> function:</p>
69<pre>// ...continued
70
71void
72work()
73    {
74    boost::<span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span> error;
75    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)) );
76    t.<span class="RenoLink"><a href="http://www.boost.org/doc/html/boost/thread.html">join</a></span>();
77    if( error )
78        boost::<span class="RenoLink"><a href="rethrow_exception.html">rethrow_exception</a></span>(error);
79    }</pre>
80<p>Note that <span class="RenoLink"><a href="current_exception.html">current_exception</a></span> could fail to copy the original exception object in the following cases:</p>
81<div><ul><li> if there is not enough memory, in which case the returned <span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span> points to an instance of std::bad_alloc, or</li>
82<li> if <span class="RenoLink"><a href="enable_current_exception.html">enable_current_exception</a></span> was not used in the throw-expression passed to the original throw statement and the current implementation does not have the necessary compiler-specific support to copy the exception automatically, in which case the returned <span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span> points to an instance of <span class="RenoLink"><a href="unknown_exception.html">unknown_exception</a></span>.</li>
83</ul></div>
84<p>Regardless, the use of <span class="RenoLink"><a href="current_exception.html">current_exception</a></span> and <span class="RenoLink"><a href="rethrow_exception.html">rethrow_exception</a></span> in the above examples is well-formed.</p>
85</div></div><div class="RenoAutoDIV"><div class="RenoHR"><hr/></div>
86See also: <span class="RenoPageList"><a href="boost-exception.html">Boost Exception</a></span>
87</div>
88<!-- Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc. -->
89<!-- Distributed under the Boost Software License, Version 1.0. (See accompanying -->
90<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
91<div id="footer">
92<p>
93<a class="logo" href="http://jigsaw.w3.org/css-validator/check/referer"><img class="logo_pic" src="valid-css.png" alt="Valid CSS" height="31" width="88"/></a>
94<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>
95<small>Copyright (c) 2006-2009 by Emil Dotchevski and Reverge Studios, Inc.<br/>
96Distributed under the <a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License, Version 1.0</a>.</small>
97</p>
98</div>
99</div>
100</div>
101</div>
102</body>
103</html>
104