mirror of
https://github.com/boostorg/exception.git
synced 2025-07-12 20:16:34 +02:00
74 lines
4.0 KiB
HTML
74 lines
4.0 KiB
HTML
![]() |
<!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: Adding of Arbitrary Data at the Point of the Throw</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"><h3>Adding of Arbitrary Data at the Point of the Throw</h3>
|
||
|
<p>The following example demonstrates how <tt>errno</tt> can be stored in exception objects using Boost Exception:</p>
|
||
|
<pre>#include <<span class="RenoLink"><a href="exception_hpp.html">boost/exception.hpp</a></span>>
|
||
|
#include <errno.h>
|
||
|
#include <iostream>
|
||
|
|
||
|
typedef boost::<span class="RenoLink"><a href="error_info.html">error_info</a></span><struct tag_errno,int> errno_info; //(1)
|
||
|
|
||
|
class my_error: public boost::<span class="RenoLink"><a href="exception.html">exception</a></span>, public std::exception { }; //(2)
|
||
|
|
||
|
void
|
||
|
f()
|
||
|
{
|
||
|
throw my_error() << errno_info(errno); //(3)
|
||
|
}
|
||
|
</pre>
|
||
|
<p>First, we instantiate the <tt><span class="RenoLink"><a href="error_info.html">error_info</a></span></tt> template using a unique identifier -- <tt>tag_errno</tt>, and the type of the info it identifies -- <tt>int</tt>. This provides compile-time type safety for the various values stored in exception objects.</p>
|
||
|
<p>Second, we define class <tt>my_error</tt>, which derives from <tt>boost::<span class="RenoLink"><a href="exception.html">exception</a></span></tt>.</p>
|
||
|
<p>Finally, (3) illustrates how the <tt>typedef</tt> from (1) can be used with <tt><span class="RenoLink"><a href="operator_shl_exception.html">operator<<</a></span>()</tt> to store values in exception objects at the point of the throw.</p>
|
||
|
<p>The stored <tt>errno</tt> value can be recovered at a later time like this:</p>
|
||
|
<pre>// ...continued
|
||
|
|
||
|
void
|
||
|
g()
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
f();
|
||
|
}
|
||
|
catch(
|
||
|
my_error & x )
|
||
|
{
|
||
|
if( boost::shared_ptr<int const> err=boost::<span class="RenoLink"><a href="get_error_info.html">get_error_info</a></span><errno_info>(x) )
|
||
|
std::cerr << "Error code: " << *err;
|
||
|
}
|
||
|
}</pre>
|
||
|
<p>The <tt><span class="RenoLink"><a href="get_error_info.html">get_error_info</a></span>()</tt> function template is instantiated with the <tt>typedef</tt> from (1), and is passed an exception object of any type that derives publicly from <tt>boost::<span class="RenoLink"><a href="exception.html">exception</a></span></tt>. If the exception object contains the requested value, the returned <tt><span class="RenoLink"><a href="http://www.boost.org/libs/smart_ptr/shared_ptr.htm">shared_ptr</a></span></tt> will point to it; otherwise an empty <tt><span class="RenoLink"><a href="http://www.boost.org/libs/smart_ptr/shared_ptr.htm">shared_ptr</a></span></tt> is returned.</p>
|
||
|
</div><h3>See also:</h3>
|
||
|
<div class="RenoPageList"><a href="transporting_data.html">Tutorial: Transporting of Arbitrary Data to the Catch Site<br/>
|
||
|
</a></div>
|
||
|
<div id="footer">
|
||
|
<p> </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>
|