diff --git a/doc/BOOST_ERROR_INFO.html b/doc/BOOST_ERROR_INFO.html new file mode 100644 index 0000000..a83038c --- /dev/null +++ b/doc/BOOST_ERROR_INFO.html @@ -0,0 +1,58 @@ + + +
+ +#include <boost/exception/info.hpp>
+namespace
+boost
+ {
+ typedef error_info<struct tag_throw_function,char const *> throw_function;
+ typedef error_info<struct tag_throw_file,char const *> throw_file;
+ typedef error_info<struct tag_throw_line,int> throw_line;
+
+ #define BOOST_ERROR_INFO\
+ ::boost::throw_function(BOOST_CURRENT_FUNCTION) <<\
+ ::boost::throw_file(__FILE__) <<\
+ ::boost::throw_line((int)__LINE__)
+ }
+This macro is designed to be used with operator<< when throwing a boost::exception, to store information about the location of the throw statement. It can be chained with other error_infos in a single throw expression.
+The purpose of Boost Exception is to ease the design of exception class hierarchies and to help write exception handling and error reporting code.
+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 classes. 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.
+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.
+Boost Exception also supports N2179-style copying of exception objects, implemented non-intrusively and automatically by the boost::throw_exception function.
+Boost Exception was accepted as a Boost library on November 7 2007, however it has not yet been part of an official Boost release. Current version can be downloaded from Boost SVN.
+#include <boost/exception.hpp>
+namespace +boost + { + class + exception + { + public: + + virtual char const * diagnostic_information() const throw(); + virtual char const * what() const throw(); + + protected: + + exception(); + exception( exception const & x ); + ~exception(); + }; + + template <class Tag,class T> + class + error_info + { + public: + + typedef T value_type; + + error_info( value_type const & ); + }; + + template <class ErrorInfo,class E> + shared_ptr<typename ErrorInfo::value_type const> get_error_info( E const & x ); + + typedef error_info<struct tag_throw_function,char const *> throw_function; + typedef error_info<struct tag_throw_file,char const *> throw_file; + typedef error_info<struct tag_throw_line,int> throw_line; + + #define BOOST_ERROR_INFO\ + ::boost::throw_function(BOOST_CURRENT_FUNCTION) <<\ + ::boost::throw_file(__FILE__) <<\ + ::boost::throw_line((int)__LINE__) + + template <class E, class Tag, class T> + E const & operator<<( E const & x, error_info<Tag,T> const & v ); + + template <class E, class Tag1, class T1, ..., class TagN, class TN> + E const & operator<<( E const & x, + tuple< + error_info<Tag1,T1>, + ..., + error_info<TagN,TN> > const & v ); + + template <class T> + ---unspecified--- enable_error_info( T const & x ); + + std::string diagnostic_information( std::exception const & x ); + + class + unknown_exception: + public std::exception + public boost::exception + { + ---unspecified--- + }; + + typedef ---unspecified--- exception_ptr; + + template <class T> + exception_ptr copy_exception( T const & e ); + + exception_ptr current_exception(); + + void rethrow_exception( exception_ptr const & ep ); + + template <class T> + ---unspecified--- enable_current_exception( T const & e ); + + #ifdef BOOST_NO_EXCEPTIONS + + void throw_exception( std::exception const & e ); // user defined + + #else + + template <class E> + void throw_exception( E const & e ); + + #endif + }+
#include <boost/exception/exception.hpp>
+namespace
+boost
+ {
+ class
+ exception
+ {
+ public:
+
+ virtual char const * diagnostic_information() const throw();
+ virtual char const * what() const throw();
+
+ protected:
+
+ exception();
+ exception( exception const & x );
+ ~exception();
+ };
+ }
+Class boost::exception is designed to be used as a universal base for user-defined exception types.
+An object of any type deriving from boost::exception can store data of arbitrary types, using the error_info wrapper and operator<<.
+To retrieve data from a boost::exception object, use the get_error_info function template.
+Nothing.
+~exception();
+Frees all resources associated with a boost::exception object.
+Nothing.
+virtual char const * diagnostic_information() const throw();
+An string representation of all data stored in the boost::exception object by the operator<< function. See "Tutorial: Diagnostic Information" for details.
+Nothing.
+The return value remains valid until the exception object from which it is obtained is destroyed or modified.
+virtual char const * what() const throw();
+The default implementation is equivalent to return exception::diagnostic_information(). The signature of boost::exception::what is identical to the familiar std::exception::what function, so that when an exception type that derives both std::exception and boost::exception overrides the what function, it overrides both std::exception::what and boost::exception::what.
+Nothing.
+The return value remains valid until the exception object from which it is obtained is destroyed or modified.
+#include <boost/exception/info.hpp>
+namespace
+boost
+ {
+ template <class Tag,class T>
+ class
+ error_info
+ {
+ public:
+
+ typedef T value_type;
+
+ error_info( value_type const & );
+ };
+ }
+T must have accessible copy constructor and must not be a reference.
+This class template is used to associate a Tag type with a value type T. Objects of type error_info<Tag,T> can be passed to operator<< to be stored in objects of type boost::exception.
+The header <boost/exception/error_info.hpp> provides a declaration of the error_info template, which is sufficient for the purpose of typedefing an instance for specific Tag and T, like this:
+#include <boost/exception/error_info.hpp> + +typedef boost::error_info<struct tag_errno,int> errno_info;+
Of course, to actually add an errno_info object to exceptions using operator<<, or to retrieve it using get_error_info, you must first #include <boost/exception/info.hpp>.
+#include <boost/exception/info.hpp>
namespace
+boost
+ {
+ template <class E, class Tag, class T>
+ E const & operator<<( E const & x, error_info<Tag,T> const & v );
+ }
+E must be boost::exception, or a type that derives (indirectly) from boost::exception.
+Stores a copy of v into x. If x already contains data of type error_info<Tag1,T1>, that data is overwritten.
+x.
+std::bad_alloc, or any exception emitted by the T copy constructor.
+#include <boost/exception/info.hpp>
+namespace
+boost
+ {
+ template <class ErrorInfo,class E>
+ shared_ptr<typename ErrorInfo::value_type const> get_error_info( E const & x );
+ }
+The type of the x object must derive from boost::exception; ErrorInfo must be an instance of the error_info template.
+If x does not store an object of type ErrorInfo, returns an empty shared_ptr; otherwise returns pointer to the stored value. Use operator<< to store values in exception objects.
+Nothing.
+#include <boost/exception/enable_error_info.hpp>
+namespace
+boost
+ {
+ template <class T>
+ ---unspecified--- enable_error_info( T const & x );
+ }
+T must be a user-defined type with accessible no-throw copy constructor.
+An object of unspecified type with no-throw copy semantics, which derives publicly from both T, and class boost::exception. The T sub-object is initialized from x by the T copy constructor. If T already derives from boost::exception, then the type of the returned object does not derive boost::exception.
+Nothing.
+#include <boost/exception/info.hpp>
+namespace
+boost
+ {
+ typedef error_info<struct tag_throw_function,char const *> throw_function;
+ typedef error_info<struct tag_throw_file,char const *> throw_file;
+ typedef error_info<struct tag_throw_line,int> throw_line;
+
+ #define BOOST_ERROR_INFO\
+ ::boost::throw_function(BOOST_CURRENT_FUNCTION) <<\
+ ::boost::throw_file(__FILE__) <<\
+ ::boost::throw_line((int)__LINE__)
+ }
+This macro is designed to be used with operator<< when throwing a boost::exception, to store information about the location of the throw statement. It can be chained with other error_infos in a single throw expression.
+#include <boost/exception_ptr.hpp>
+namespace
+boost
+ {
+ typedef ---unspecified--- exception_ptr;
+ }
+The exception_ptr type can be used to refer to a copy of an exception object. It is Default Constructible, Copy Constructible, Assignable and Equality Comparable; exception_ptr's operations do not throw.
+Two instances of exception_ptr are equivalent and compare equal if and only if they refer to the same exception.
+The default constructor of exception_ptr produces the null value of the type. The null value is equivalent only to itself.
+exception_ptr objects are returned by current_exception and copy_exception.
+#include <boost/exception/enable_current_exception.hpp>
+namespace
+boost
+ {
+ template <class T>
+ ---unspecified--- enable_current_exception( T const & e );
+ }
+T must have an accessible no-throw copy constructor
+An object of unspecified type which derives publicly from T. That is, the returned object can be intercepted by a catch(T &).
+This function is designed to be used directly in a throw-expression to enable the exception_ptr support in Boost Exception. For example:
+class
+my_exception:
+ public std::exception
+ {
+ };
+
+....
+throw boost::enable_current_exception(my_exception());
+Unless enable_current_exception is called at the time an exception object is used in a throw-expression, an attempt to copy it using current_exception may return an exception_ptr which refers to an instance of unknown_exception. See current_exception for details.
+Instead of using the throw keyword directly, it is preferable to call boost::throw_exception. This is guaranteed to throw an exception that derives from boost::exception and supports exception_ptr functionality.
+#include <boost/exception_ptr.hpp>
+namespace
+boost
+ {
+ exception_ptr current_exception();
+ }
+The current_exception function must not be called outside of a catch block.
+Nothing.
+#include <boost/exception_ptr.hpp>
+namespace
+boost
+ {
+ template <class T>
+ exception_ptr copy_exception( T const & e );
+ }
+As if try { throw e; } catch( ... ) { return current_exception(); }
+#include <boost/exception_ptr.hpp>
+namespace
+boost
+ {
+ void rethrow_exception( exception_ptr const & ep );
+ }
+ep shall not be null.
+The exception to which ep refers.
+#include <boost/exception_ptr.hpp>
+namespace
+boost
+ {
+ class
+ unknown_exception:
+ public std::exception
+ public boost::exception
+ {
+ ---unspecified---
+ };
+ }
+This type is used by the exception_ptr support in Boost Exception. Please see current_exception.
+#include <boost/exception/diagnostic_information.hpp>
+namespace
+boost
+ {
+ std::string diagnostic_information( std::exception const & x );
+ }
+If dynamic_cast<boost::exception *>(&x) is not null, the returned string is initialized by a call to exception::diagnostic_information; otherwise, the returned string combines the output of x.what() and typeid(x).name().
+#include <boost/throw_exception.hpp>
+namespace
+boost
+ {
+ #ifdef BOOST_NO_EXCEPTIONS
+
+ void throw_exception( std::exception const & e ); // user defined
+
+ #else
+
+ template <class E>
+ void throw_exception( E const & e );
+
+ #endif
+ }
+E must derive publicly from std::exception.
+#include <exception>
+
+namespace
+boost
+ {
+ std::string diagnostic_information( std::exception const & x );
+ }
+#include <boost/exception_ptr.hpp>
+namespace
+boost
+ {
+ template <class T>
+ exception_ptr copy_exception( T const & e );
+ }
+As if try { throw e; } catch( ... ) { return current_exception(); }
+#include <boost/exception_ptr.hpp>
+namespace
+boost
+ {
+ exception_ptr current_exception();
+ }
+The current_exception function must not be called outside of a catch block.
+Nothing.
+#include <boost/exception/diagnostic_information.hpp>
+namespace
+boost
+ {
+ std::string diagnostic_information( std::exception const & x );
+ }
+If dynamic_cast<boost::exception *>(&x) is not null, the returned string is initialized by a call to exception::diagnostic_information; otherwise, the returned string combines the output of x.what() and typeid(x).name().
+#include <boost/exception/enable_current_exception.hpp>
+namespace
+boost
+ {
+ template <class T>
+ ---unspecified--- enable_current_exception( T const & e );
+ }
+T must have an accessible no-throw copy constructor
+An object of unspecified type which derives publicly from T. That is, the returned object can be intercepted by a catch(T &).
+This function is designed to be used directly in a throw-expression to enable the exception_ptr support in Boost Exception. For example:
+class
+my_exception:
+ public std::exception
+ {
+ };
+
+....
+throw boost::enable_current_exception(my_exception());
+Unless enable_current_exception is called at the time an exception object is used in a throw-expression, an attempt to copy it using current_exception may return an exception_ptr which refers to an instance of unknown_exception. See current_exception for details.
+Instead of using the throw keyword directly, it is preferable to call boost::throw_exception. This is guaranteed to throw an exception that derives from boost::exception and supports exception_ptr functionality.
+#include <boost/exception/enable_error_info.hpp>
+namespace
+boost
+ {
+ template <class T>
+ ---unspecified--- enable_error_info( T const & x );
+ }
+T must be a user-defined type with accessible no-throw copy constructor.
+An object of unspecified type with no-throw copy semantics, which derives publicly from both T, and class boost::exception. The T sub-object is initialized from x by the T copy constructor. If T already derives from boost::exception, then the type of the returned object does not derive boost::exception.
+Nothing.
+#include <boost/exception/info.hpp>
+namespace
+boost
+ {
+ template <class Tag,class T>
+ class
+ error_info
+ {
+ public:
+
+ typedef T value_type;
+
+ error_info( value_type const & );
+ };
+ }
+T must have accessible copy constructor and must not be a reference.
+This class template is used to associate a Tag type with a value type T. Objects of type error_info<Tag,T> can be passed to operator<< to be stored in objects of type boost::exception.
+The header <boost/exception/error_info.hpp> provides a declaration of the error_info template, which is sufficient for the purpose of typedefing an instance for specific Tag and T, like this:
+#include <boost/exception/error_info.hpp> + +typedef boost::error_info<struct tag_errno,int> errno_info;+
Of course, to actually add an errno_info object to exceptions using operator<<, or to retrieve it using get_error_info, you must first #include <boost/exception/info.hpp>.
+#include <boost/exception/exception.hpp>
+namespace
+boost
+ {
+ class
+ exception
+ {
+ public:
+
+ virtual char const * diagnostic_information() const throw();
+ virtual char const * what() const throw();
+
+ protected:
+
+ exception();
+ exception( exception const & x );
+ ~exception();
+ };
+ }
+Class boost::exception is designed to be used as a universal base for user-defined exception types.
+An object of any type deriving from boost::exception can store data of arbitrary types, using the error_info wrapper and operator<<.
+To retrieve data from a boost::exception object, use the get_error_info function template.
+Nothing.
+~exception();
+Frees all resources associated with a boost::exception object.
+Nothing.
+virtual char const * diagnostic_information() const throw();
+An string representation of all data stored in the boost::exception object by the operator<< function. See "Tutorial: Diagnostic Information" for details.
+Nothing.
+The return value remains valid until the exception object from which it is obtained is destroyed or modified.
+virtual char const * what() const throw();
+The default implementation is equivalent to return exception::diagnostic_information(). The signature of boost::exception::what is identical to the familiar std::exception::what function, so that when an exception type that derives both std::exception and boost::exception overrides the what function, it overrides both std::exception::what and boost::exception::what.
+Nothing.
+The return value remains valid until the exception object from which it is obtained is destroyed or modified.
+#include <boost/exception/exception.hpp> + +namespace +boost + { + class + unknown_exception: + public std::exception + public boost::exception + { + ---unspecified--- + }; + + typedef ---unspecified--- exception_ptr; + + template <class T> + exception_ptr copy_exception( T const & e ); + + exception_ptr current_exception(); + + void rethrow_exception( exception_ptr const & ep ); + }+
Nothing.
+~exception();
+Frees all resources associated with a boost::exception object.
+Nothing.
+virtual char const * diagnostic_information() const throw();
+An string representation of all data stored in the boost::exception object by the operator<< function. See "Tutorial: Diagnostic Information" for details.
+Nothing.
+The return value remains valid until the exception object from which it is obtained is destroyed or modified.
+#include <boost/exception/exception.hpp> + +namespace +boost + { + template <class T> + ---unspecified--- enable_error_info( T const & x ); + }+
#include <boost/exception/exception.hpp> + +namespace +boost + { + template <class T> + ---unspecified--- enable_current_exception( T const & e ); + }+
#include <boost/tuple/tuple.hpp>
+
+namespace
+boost
+ {
+ template <class E, class Tag1, class T1, ..., class TagN, class TN>
+ E const & operator<<( E const & x,
+ tuple<
+ error_info<Tag1,T1>,
+ ...,
+ error_info<TagN,TN> > const & v );
+ }
+#include <boost/exception/exception.hpp> +#include <boost/current_function.hpp> +#include <boost/shared_ptr.hpp> + +namespace +boost + { + template <class Tag,class T> + class + error_info + { + public: + + typedef T value_type; + + error_info( value_type const & ); + }; + + template <class ErrorInfo,class E> + shared_ptr<typename ErrorInfo::value_type const> get_error_info( E const & x ); + + typedef error_info<struct tag_throw_function,char const *> throw_function; + typedef error_info<struct tag_throw_file,char const *> throw_file; + typedef error_info<struct tag_throw_line,int> throw_line; + + #define BOOST_ERROR_INFO\ + ::boost::throw_function(BOOST_CURRENT_FUNCTION) <<\ + ::boost::throw_file(__FILE__) <<\ + ::boost::throw_line((int)__LINE__) + + template <class E, class Tag, class T> + E const & operator<<( E const & x, error_info<Tag,T> const & v ); + }+
namespace
+boost
+ {
+ template <class Tag, class T>
+ class error_info;
+ }
+namespace
+boost
+ {
+ class
+ exception
+ {
+ public:
+
+ virtual char const * diagnostic_information() const throw();
+ virtual char const * what() const throw();
+
+ protected:
+
+ exception();
+ exception( exception const & x );
+ ~exception();
+ };
+ }
+#include <boost/exception/info.hpp> +#include <boost/exception/info_tuple.hpp> +#include <boost/exception/diagnostic_information.hpp> +#include <boost/exception_ptr.hpp> +#include <boost/throw_exception.hpp>+
#include <boost/exception_ptr.hpp>
+namespace
+boost
+ {
+ typedef ---unspecified--- exception_ptr;
+ }
+The exception_ptr type can be used to refer to a copy of an exception object. It is Default Constructible, Copy Constructible, Assignable and Equality Comparable; exception_ptr's operations do not throw.
+Two instances of exception_ptr are equivalent and compare equal if and only if they refer to the same exception.
+The default constructor of exception_ptr produces the null value of the type. The null value is equivalent only to itself.
+exception_ptr objects are returned by current_exception and copy_exception.
+virtual char const * what() const throw();
+The default implementation is equivalent to return exception::diagnostic_information(). The signature of boost::exception::what is identical to the familiar std::exception::what function, so that when an exception type that derives both std::exception and boost::exception overrides the what function, it overrides both std::exception::what and boost::exception::what.
+Nothing.
+The return value remains valid until the exception object from which it is obtained is destroyed or modified.
+#include <boost/exception/info.hpp>
+namespace
+boost
+ {
+ template <class ErrorInfo,class E>
+ shared_ptr<typename ErrorInfo::value_type const> get_error_info( E const & x );
+ }
+The type of the x object must derive from boost::exception; ErrorInfo must be an instance of the error_info template.
+If x does not store an object of type ErrorInfo, returns an empty shared_ptr; otherwise returns pointer to the stored value. Use operator<< to store values in exception objects.
+Nothing.
+boost/exception/diagnostic_information.hpp
+boost/exception/enable_current_exception.hpp
+boost/exception/enable_error_info.hpp
+boost/exception/error_info.hpp
+ + +boost/exception/info_tuple.hpp
+ + +exception::diagnostic_information
+ + + + +Tutorial: Diagnostic Information
+Tutorial: Integrating Boost Exception in Existing Exception Class Hierarchies
+Tutorial: Transporting of Arbitrary Data to the Catch Site
+Tutorial: Transporting of Exceptions between Threads
+#include <boost/exception/info.hpp>
namespace
+boost
+ {
+ template <class E, class Tag, class T>
+ E const & operator<<( E const & x, error_info<Tag,T> const & v );
+ }
+E must be boost::exception, or a type that derives (indirectly) from boost::exception.
+Stores a copy of v into x. If x already contains data of type error_info<Tag1,T1>, that data is overwritten.
+x.
+std::bad_alloc, or any exception emitted by the T copy constructor.
+#include <boost/exception/info_tuple.hpp>
+namespace
+boost
+ {
+ template <class E, class Tag1, class T1, ..., class TagN, class TN>
+ E const & operator<<( E const & x,
+ tuple<
+ error_info<Tag1,T1>,
+ ...,
+ error_info<TagN,TN> > const & v );
+ }
+E must be boost::exception, or a type that derives (indirectly) from boost::exception.
+Equivalent to x << v.get<0>() << ... << v.get<N>().
+x.
+std::bad_alloc, or any exception emitted by T1..TN copy constructor.
+#include <boost/exception_ptr.hpp>
+namespace
+boost
+ {
+ void rethrow_exception( exception_ptr const & ep );
+ }
+ep shall not be null.
+The exception to which ep refers.
+#include <boost/throw_exception.hpp>
+namespace
+boost
+ {
+ #ifdef BOOST_NO_EXCEPTIONS
+
+ void throw_exception( std::exception const & e ); // user defined
+
+ #else
+
+ template <class E>
+ void throw_exception( E const & e );
+
+ #endif
+ }
+E must derive publicly from std::exception.
+#include <boost/exception/enable_current_exception.hpp> +#include <boost/exception/enable_error_info.hpp> +#include <exception> + +namespace +boost + { + #ifdef BOOST_NO_EXCEPTIONS + + void throw_exception( std::exception const & e ); // user defined + + #else + + template <class E> + void throw_exception( E const & e ); + + #endif + }+
Class boost::exception provides a virtual member function diagnostic_information, with a signature similar to the familiar std::exception::what function. The default implementation returns a string value that is not presentable as a friendly user message, but because it is generated automatically, it is useful for debugging or logging purposes. Here is an example:
+#include <boost/exception.hpp> +#include <iostream> + +void f(); //throws unknown types that derive from boost::exception. + +void +g() + { + try + { + f(); + } + catch( + boost::exception & e ) + { + std::cerr << e.diagnostic_information(); + } + }+
The diagnostic_information member function iterates over all data objects stored in the boost::exception through operator<<. The returned string is constructed by converting each data object to string and then concatenating these strings together.
+When the error_info<Tag,T> template is instantiated, the system attempts overload resolution for an unqualified call to to_string(x), where x is of type T. If this is successful, the to_string overload is used to convert objects of type T to string.
+Otherwise, the system attempts overload resolution for s << x, where s is a std::ostringstream and x is of type T. If this is successful, the operator<< overload is used to convert objects of type T to string.
+Otherwise the system is unable to convert objects of type T to string, and an unspecified stub string value is used without issuing a compile error.
+Some exception hierarchies can not be modified to make boost::exception a base type. For this case, the enable_error_info function template can be used to make exception objects derive from boost::exception anyway. Here is an example:
+#include <boost/exception.hpp> +#include <stdexcept> + +typedef boost::error_info<struct tag_std_range_min,size_t> std_range_min; +typedef boost::error_info<struct tag_std_range_max,size_t> std_range_max; +typedef boost::error_info<struct tag_std_range_index,size_t> std_range_index; + +template <class T> +class +my_container + { + public: + + size_t size() const; + + T const & + operator[]( size_t i ) const + { + if( i > size() ) + throw boost::enable_error_info(std::range_error("Index out of range")) << + std_range_min(0) << + std_range_max(size()) << + std_range_index(i); + //.... + } + }; ++
The call to enable_error_info<T> gets us an object of unspecified type which is guaranteed to derive from both boost::exception and T. This makes it possible to use operator<< to store additional information in the exception object. The exception can be intercepted as T &, so existing exception handling will not break. It can also be intercepted as boost::exception &, so that more information can be added to the exception at a later time.
+Boost Exception supports transporting of exception objects between threads through cloning. This system is similar to N2179, but because Boost Exception can not rely on language support, the use of enable_current_exception at the time of the throw is required in order to use cloning.
+All exceptions emitted by the familiar function boost::throw_exception are guaranteed to derive from boost::exception and to support cloning.
+Here is how cloning can be enabled in a throw-expression (15.1):
+#include <boost/exception/enable_current_exception.hpp> +#include <boost/exception/info.hpp> +#include <stdio.h> +#include <errno.h> + +typedef boost::error_info<struct tag_errno,int> errno_info; + +class file_read_error: public boost::exception { }; + +void +file_read( FILE * f, void * buffer, size_t size ) + { + if( size!=fread(buffer,1,size,f) ) + throw boost::enable_current_exception(file_read_error()) << + errno_info(errno); + }+
Of course, enable_current_exception may be used with any exception type; there is no requirement that it should derive from boost::exception.
+When you catch an exception, you can call current_exception to get an exception_ptr object:
+#include <boost/exception_ptr.hpp> +#include <boost/thread.hpp> +#include <boost/bind.hpp> + +void do_work(); //throws cloning-enabled boost::exceptions + +void +worker_thread( boost::exception_ptr & error ) + { + try + { + do_work(); + error = boost::exception_ptr(); + } + catch( + ... ) + { + error = boost::current_exception(); + } + }+
In the above example, note that current_exception captures the original type of the exception object. The exception can be thrown again using the rethrow_exception function:
+// ...continued + +void +work() + { + boost::exception_ptr error; + boost::thread t( boost::bind(worker_thread,boost::ref(error)) ); + t.join(); + if( error ) + boost::rethrow_exception(error); + }+
Note that current_exception could fail to copy the original exception object in the following cases:* if there is not enough memory, in which case the returned exception_ptr points to an instance of std::bad_alloc, or
+Regardless, the use of current_exception and rethrow_exception in the above examples is well-formed.
+All exception types that derive from boost::exception can be used as type-safe containers of arbitrary data objects, while complying with the no-throw requirements (15.5.1) of the ANSI C++ standard for exception types. Data can be added to a boost::exception at the time of the throw, or at a later time.
+The following example demonstrates how errno can be stored in exception objects using Boost Exception:
+#include <boost/exception.hpp> +#include <errno.h> +#include <iostream> + +typedef boost::error_info<struct tag_errno,int> errno_info; //(1) + +class my_error: public boost::exception, public std::exception { }; //(2) + +void +f() + { + throw my_error() << errno_info(errno); //(3) + } ++
First, we instantiate the error_info template using a unique identifier -- tag_errno, and the type of the info it identifies -- int. This provides compile-time type safety for the various values stored in exception objects.
+Second, we define class my_error, which derives from boost::exception.
+Finally, (3) illustrates how the typedef from (1) can be used with operator<< to store values in exception objects at the point of the throw.
+The stored errno value can be recovered at a later time like this:
+// ...continued
+
+void
+g()
+ {
+ try
+ {
+ f();
+ }
+ catch(
+ my_error & x )
+ {
+ if( boost::shared_ptr<int const> err=boost::get_error_info<errno_info>(x) )
+ std::cerr << "Error code: " << *err;
+ }
+ }
+The get_error_info function template is instantiated with the typedef from (1), and is passed an exception object of any type that derives publicly from boost::exception. If the exception object contains the requested value, the returned shared_ptr will point to it; otherwise an empty shared_ptr is returned.
+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:
+#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("????"); + }+
We have defined an exception class file_read_error which can store a file name, so that when we catch a file_read_error object, we know which file the failure is related to. However, the file_read function does not have the file name at the time of the throw; all it has is a FILE handle.
+One possible solution is to not use FILE handles directly. We could have our own class file which stores both a FILE handle and a file name, and pass that to file_read. However, this could be problematic if we communicate with 3rd party code that does not use our class file (probably because they have their own similar class.)
+A better solution is to make class file_read_error derive (possibly indirectly) from boost::exception, and free the file_read function from the burden of storing the file name in exceptions it throws:
+#include <boost/exception.hpp> +#include <stdio.h> +#include <errno.h> + +typedef boost::error_info<struct tag_errno,int> errno_info; + +class file_read_error: public boost::exception { }; + +void +file_read( FILE * f, void * buffer, size_t size ) + { + if( size!=fread(buffer,1,size,f) ) + throw file_read_error() << errno_info(errno); + }+
If file_read detects a failure, it throws an exception which contains the information that is available at the time, namely the errno. Other relevant information, such as the file name, can be added in a context higher up the call stack, where it is known naturally:
+#include <boost/exception.hpp> +#include <boost/shared_ptr.hpp> +#include <stdio.h> +#include <string> + +typedef boost::error_info<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::exception & e ) + { + e << file_name_info(file_name); + throw; + } + }+
The above function is (almost) exception-neutral -- if an exception is emitted by any function call within the try block, parse_file does not need to do any real work, but it intercepts any boost::exception object, stores the file name, and re-throws using a throw-expression with no operand (15.1.6). The rationale for catching any boost::exception object is that the file name is relevant to any failure that occurs in parse_file, even if the failure is unrelated to file I/O.
+As usual, the stored data can be retrieved using get_error_info.
+The code snippet below demonstrates how boost::tuple can be used to bundle the name of the function that failed, together with the reported errno so that they can be added to exception objects more conveniently together:
+#include <boost/exception/info_tuple.hpp> +#include <boost/shared_ptr.hpp> +#include <stdio.h> +#include <string> +#include <errno.h> + +typedef boost::error_info<struct tag_file_name,std::string> file_name_info; +typedef boost::error_info<struct tag_function,char const *> function_info; +typedef boost::error_info<struct tag_errno,int> errno_info; +typedef boost::tuple<function_info,errno_info> clib_failure; + +class file_open_error: public boost::exception { }; + +boost::shared_ptr<FILE> +file_open( char const * name, char const * mode ) + { + if( FILE * f=fopen(name,mode) ) + return boost::shared_ptr<FILE>(f,fclose); + else + throw file_open_error() << + file_name_info(name) << + clib_failure("fopen",errno); + }+
Note that the members of a boost::tuple are stored separately in exception objects; they can only be retrieved individually, using get_error_info.
+#include <boost/exception_ptr.hpp>
+namespace
+boost
+ {
+ class
+ unknown_exception:
+ public std::exception
+ public boost::exception
+ {
+ ---unspecified---
+ };
+ }
+This type is used by the exception_ptr support in Boost Exception. Please see current_exception.
+