mirror of
https://github.com/boostorg/exception.git
synced 2025-07-12 12:06:33 +02:00
117 lines
5.7 KiB
HTML
117 lines
5.7 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 to Active Exception Objects</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 to Active Exception Objects</h3>
|
|
<p>Sometimes the throw site does not have all the information that is needed at the catch site to make sense of what went wrong. Here is an example:</p>
|
|
<pre>#include <stdio.h>
|
|
#include <string>
|
|
|
|
class
|
|
file_read_error
|
|
{
|
|
public:
|
|
|
|
explicit
|
|
file_read_error( std::string const & fn ):
|
|
fn_(fn)
|
|
{
|
|
};
|
|
|
|
std::string const &
|
|
file_name() const
|
|
{
|
|
return fn_;
|
|
}
|
|
|
|
private:
|
|
|
|
std::string fn_;
|
|
};
|
|
|
|
void
|
|
file_read( FILE * f, void * buffer, size_t size )
|
|
{
|
|
if( size!=fread(buffer,1,size,f) )
|
|
throw file_read_error("????");
|
|
}</pre>
|
|
<p>We have defined an exception class <tt>file_read_error</tt> which can store a file name, so that when we catch a <tt>file_read_error</tt> object, we know which file the failure is related to. However, the <tt>file_read</tt> function does not have the file name at the time of the throw; all it has is a <tt>FILE</tt> handle.</p>
|
|
<p>One possible solution is to not use <tt>FILE</tt> handles directly. We could have our own <tt>class file</tt> which stores both a <tt>FILE</tt> handle and a file name, and pass that to <tt>file_read()</tt>. However, this could be problematic if we communicate with 3rd party code that does not use our <tt>class file</tt> (probably because they have their own similar class.)</p>
|
|
<p>A better solution is to make class <tt>file_read_error</tt> derive (possibly indirectly) from <tt>boost::<span class="RenoLink"><a href="exception.html">exception</a></span></tt>, and free the <tt>file_read()</tt> function from the burden of storing the file name in exceptions it throws:</p>
|
|
<pre>#include <<span class="RenoLink"><a href="exception_hpp.html">boost/exception.hpp</a></span>>
|
|
#include <stdio.h>
|
|
#include <errno.h>
|
|
|
|
typedef boost::<span class="RenoLink"><a href="error_info.html">error_info</a></span><struct tag_errno,int> 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 file_read_error() << errno_info(errno);
|
|
}</pre>
|
|
<p>If <tt>file_read()</tt> detects a failure, it throws an exception which contains the information that is available at the time, namely the <tt>errno</tt>. Other relevant information, such as the file name, can be added in a context higher up the call stack, where it is known naturally:</p>
|
|
<pre>#include <<span class="RenoLink"><a href="exception_hpp.html">boost/exception.hpp</a></span>>
|
|
#include <boost/shared_ptr.hpp>
|
|
#include <stdio.h>
|
|
#include <string>
|
|
|
|
typedef boost::<span class="RenoLink"><a href="error_info.html">error_info</a></span><struct tag_file_name,std::string> file_name_info;
|
|
|
|
boost::shared_ptr<FILE> file_open( char const * file_name, char const * mode );
|
|
void file_read( FILE * f, void * buffer, size_t size );
|
|
|
|
void
|
|
parse_file( char const * file_name )
|
|
{
|
|
boost::shared_ptr<FILE> f = file_open(file_name,"rb");
|
|
assert(f);
|
|
try
|
|
{
|
|
char buf[1024];
|
|
file_read( f.get(), buf, sizeof(buf) );
|
|
}
|
|
catch(
|
|
boost::<span class="RenoLink"><a href="exception.html">exception</a></span> & e )
|
|
{
|
|
e << file_name_info(file_name);
|
|
throw;
|
|
}
|
|
}</pre>
|
|
<p>The above function is (almost) exception-neutral -- if an exception is emitted by any function call within the <tt>try</tt> block, <tt>parse_file()</tt> does not need to do any real work, but it intercepts any <tt>boost::<span class="RenoLink"><a href="exception.html">exception</a></span></tt> object, stores the file name, and re-throws using a throw-expression with no operand (15.1.6). The rationale for catching any <tt>boost::<span class="RenoLink"><a href="exception.html">exception</a></span></tt> object is that the file name is relevant to any failure that occurs in <tt>parse_file()</tt>, <i>even if the failure is unrelated to file I/O</i>.</p>
|
|
<p>As usual, the stored data can be retrieved using <tt><span class="RenoLink"><a href="get_error_info.html">get_error_info</a></span>()</tt>.</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>
|