diff --git a/doc/boost_exception_all_hpp.html b/doc/boost_exception_all_hpp.html new file mode 100644 index 0000000..0e932d4 --- /dev/null +++ b/doc/boost_exception_all_hpp.html @@ -0,0 +1,64 @@ + + +
+ +#include <boost/exception/diagnostic_information.hpp>
+#include <boost/exception/error_info.hpp>
+#include <boost/exception/exception.hpp>
+#include <boost/exception/get_error_info.hpp>
+#include <boost/exception/info.hpp>
+#include <boost/exception/info_tuple.hpp>
+#include <boost/exception_ptr.hpp>
+#include <boost/exception/errinfo_api_function.hpp>
+#include <boost/exception/errinfo_at_line.hpp>
+#include <boost/exception/errinfo_errno.hpp>
+#include <boost/exception/errinfo_file_handle.hpp>
+#include <boost/exception/errinfo_file_name.hpp>
+#include <boost/exception/errinfo_file_open_mode.hpp>
+#include <boost/exception/errinfo_type_info_name.hpp>
+#include <boost/exception/error_info.hpp> + +namespace +boost + { + typedef error_info<struct errinfo_api_function_,int> errinfo_api_function; + }+
#include <boost/exception/error_info.hpp> + +namespace +boost + { + typedef error_info<struct errinfo_at_line_,int> errinfo_at_line; + }+
#include <boost/exception/error_info.hpp> +#include <errno.h> + +namespace +boost + { + typedef error_info<struct errinfo_errno_,int> errinfo_errno; + }+
#include <boost/exception/error_info.hpp> + +namespace +boost + { + template <class> class weak_ptr; + typedef error_info<struct errinfo_file_handle_,weak_ptr<FILE> > errinfo_file_handle; + }+
#include <boost/exception/error_info.hpp> +#include <string> + +namespace +boost + { + typedef error_info<struct errinfo_file_name_,std::string> errinfo_file_name; + }+
#include <boost/exception/error_info.hpp> +#include <string> + +namespace +boost + { + typedef error_info<struct errinfo_file_open_mode_,std::string> errinfo_file_open_mode; + }+
#include <boost/exception/error_info.hpp> +#include <string> + +namespace +boost + { + typedef error_info<struct errinfo_type_info_name_,std::string> errinfo_type_info_name; + }+
#include <boost/exception/diagnostic_information.hpp>
-#include <boost/exception/error_info.hpp>
-#include <boost/exception/exception.hpp>
-#include <boost/exception/get_error_info.hpp>
-#include <boost/exception/info.hpp>
-#include <boost/exception/info_tuple.hpp>
-#include <boost/exception_ptr.hpp>
-This header has been deprecated.
+Please #include <boost/exception/all.hpp> instead.
+this is a possible output from the diagnostic_information function, as used in libs/exception/example/example_io.cpp:
-example_io.cpp(83): Throw in function class boost::shared_ptr<struct _iobuf> __cdecl my_fopen(const char *,const char *) -Dynamic exception type: class boost::exception_detail::clone_impl<class fopen_error> +example_io.cpp(70): Throw in function class boost::shared_ptr<struct _iobuf> __cdecl my_fopen(const char *,const char *) +Dynamic exception type: class boost::exception_detail::clone_impl<struct fopen_error> std::exception::what: example_io error -[struct tag_errno *] = 2, OS says "No such file or directory" -[struct tag_file_name *] = tmp1.txt -[struct tag_function *] = fopen -[struct tag_open_mode *] = rb+[struct boost::errinfo_api_function_ *] = fopen +[struct boost::errinfo_errno_ *] = 2, "No such file or directory" +[struct boost::errinfo_file_name_ *] = tmp1.txt +[struct boost::errinfo_file_open_mode_ *] = rb
#include <boost/exception/errinfo_api_function.hpp>
+#include <boost/exception/error_info.hpp> + +namespace +boost + { + typedef error_info<struct errinfo_api_function_,int> errinfo_api_function; + }+
This type is designed to be used as a standard error_info instance for transporting the name of a relevant API function (which does not use exceptions to report errors) in exceptions deriving from boost::exception.
+#include <boost/exception/errinfo_api_function.hpp> +#include <boost/exception/errinfo_at_line.hpp> +#include <boost/exception/errinfo_errno.hpp> +#include <boost/exception/errinfo_file_handle.hpp> +#include <boost/exception/errinfo_file_name.hpp> +#include <boost/exception/errinfo_file_open_mode.hpp> +#include <boost/exception/info.hpp> +#include <boost/throw_exception.hpp> +#include <boost/shared_ptr.hpp> +#include <boost/weak_ptr.hpp> +#include <stdio.h> +#include <errno.h> +#include <exception> + +struct error : virtual std::exception, virtual boost::exception { }; +struct file_error : virtual error { }; +struct file_open_error: virtual file_error { }; +struct file_read_error: virtual file_error { }; + +boost::shared_ptr<FILE> +open_file( char const * file, char const * mode ) + { + if( FILE * f=fopen(file,mode) ) + return boost::shared_ptr<FILE>(f,fclose); + else + BOOST_THROW_EXCEPTION( + file_open_error() << + boost::errinfo_api_function("fopen") << + boost::errinfo_errno(errno) << + boost::errinfo_file_name(file) << + boost::errinfo_file_open_mode(mode) ); + } + +size_t +read_file( boost::shared_ptr<FILE> const & f, void * buf, size_t size ) + { + size_t nr=fread(buf,1,size,f.get()); + if( ferror(f.get()) ) + BOOST_THROW_EXCEPTION( + file_read_error() << + boost::errinfo_api_function("fread") << + boost::errinfo_errno(errno) << + boost::errinfo_file_handle(f) ); + return nr; + }+
#include <boost/exception/errinfo_at_line.hpp>
+#include <boost/exception/error_info.hpp> + +namespace +boost + { + typedef error_info<struct errinfo_at_line_,int> errinfo_at_line; + }+
This type is designed to be used as a standard error_info instance for transporting a relevant text file line number, for example in parse error exceptions deriving from boost::exception.
+#include <boost/exception/errinfo_api_function.hpp> +#include <boost/exception/errinfo_at_line.hpp> +#include <boost/exception/errinfo_errno.hpp> +#include <boost/exception/errinfo_file_handle.hpp> +#include <boost/exception/errinfo_file_name.hpp> +#include <boost/exception/errinfo_file_open_mode.hpp> +#include <boost/exception/info.hpp> +#include <boost/throw_exception.hpp> +#include <boost/shared_ptr.hpp> +#include <boost/weak_ptr.hpp> +#include <stdio.h> +#include <errno.h> +#include <exception> + +struct error : virtual std::exception, virtual boost::exception { }; +struct file_error : virtual error { }; +struct file_open_error: virtual file_error { }; +struct file_read_error: virtual file_error { }; + +boost::shared_ptr<FILE> +open_file( char const * file, char const * mode ) + { + if( FILE * f=fopen(file,mode) ) + return boost::shared_ptr<FILE>(f,fclose); + else + BOOST_THROW_EXCEPTION( + file_open_error() << + boost::errinfo_api_function("fopen") << + boost::errinfo_errno(errno) << + boost::errinfo_file_name(file) << + boost::errinfo_file_open_mode(mode) ); + } + +size_t +read_file( boost::shared_ptr<FILE> const & f, void * buf, size_t size ) + { + size_t nr=fread(buf,1,size,f.get()); + if( ferror(f.get()) ) + BOOST_THROW_EXCEPTION( + file_read_error() << + boost::errinfo_api_function("fread") << + boost::errinfo_errno(errno) << + boost::errinfo_file_handle(f) ); + return nr; + }+
#include <boost/exception/errinfo_errno.hpp>
+#include <boost/exception/error_info.hpp> +#include <errno.h> + +namespace +boost + { + typedef error_info<struct errinfo_errno_,int> errinfo_errno; + }+
This type is designed to be used as a standard error_info instance for transporting a relevant errno value in exceptions deriving from boost::exception.
+#include <boost/exception/errinfo_api_function.hpp> +#include <boost/exception/errinfo_at_line.hpp> +#include <boost/exception/errinfo_errno.hpp> +#include <boost/exception/errinfo_file_handle.hpp> +#include <boost/exception/errinfo_file_name.hpp> +#include <boost/exception/errinfo_file_open_mode.hpp> +#include <boost/exception/info.hpp> +#include <boost/throw_exception.hpp> +#include <boost/shared_ptr.hpp> +#include <boost/weak_ptr.hpp> +#include <stdio.h> +#include <errno.h> +#include <exception> + +struct error : virtual std::exception, virtual boost::exception { }; +struct file_error : virtual error { }; +struct file_open_error: virtual file_error { }; +struct file_read_error: virtual file_error { }; + +boost::shared_ptr<FILE> +open_file( char const * file, char const * mode ) + { + if( FILE * f=fopen(file,mode) ) + return boost::shared_ptr<FILE>(f,fclose); + else + BOOST_THROW_EXCEPTION( + file_open_error() << + boost::errinfo_api_function("fopen") << + boost::errinfo_errno(errno) << + boost::errinfo_file_name(file) << + boost::errinfo_file_open_mode(mode) ); + } + +size_t +read_file( boost::shared_ptr<FILE> const & f, void * buf, size_t size ) + { + size_t nr=fread(buf,1,size,f.get()); + if( ferror(f.get()) ) + BOOST_THROW_EXCEPTION( + file_read_error() << + boost::errinfo_api_function("fread") << + boost::errinfo_errno(errno) << + boost::errinfo_file_handle(f) ); + return nr; + }+
#include <boost/exception/errinfo_file_handle.hpp>
+#include <boost/exception/error_info.hpp> + +namespace +boost + { + template <class> class weak_ptr; + typedef error_info<struct errinfo_file_handle_,weak_ptr<FILE> > errinfo_file_handle; + }+
This type is designed to be used as a standard error_info instance for transporting a relevant FILE pointer managed by a boost::shared_ptr<FILE> in exceptions deriving from boost::exception.
+#include <boost/exception/errinfo_api_function.hpp> +#include <boost/exception/errinfo_at_line.hpp> +#include <boost/exception/errinfo_errno.hpp> +#include <boost/exception/errinfo_file_handle.hpp> +#include <boost/exception/errinfo_file_name.hpp> +#include <boost/exception/errinfo_file_open_mode.hpp> +#include <boost/exception/info.hpp> +#include <boost/throw_exception.hpp> +#include <boost/shared_ptr.hpp> +#include <boost/weak_ptr.hpp> +#include <stdio.h> +#include <errno.h> +#include <exception> + +struct error : virtual std::exception, virtual boost::exception { }; +struct file_error : virtual error { }; +struct file_open_error: virtual file_error { }; +struct file_read_error: virtual file_error { }; + +boost::shared_ptr<FILE> +open_file( char const * file, char const * mode ) + { + if( FILE * f=fopen(file,mode) ) + return boost::shared_ptr<FILE>(f,fclose); + else + BOOST_THROW_EXCEPTION( + file_open_error() << + boost::errinfo_api_function("fopen") << + boost::errinfo_errno(errno) << + boost::errinfo_file_name(file) << + boost::errinfo_file_open_mode(mode) ); + } + +size_t +read_file( boost::shared_ptr<FILE> const & f, void * buf, size_t size ) + { + size_t nr=fread(buf,1,size,f.get()); + if( ferror(f.get()) ) + BOOST_THROW_EXCEPTION( + file_read_error() << + boost::errinfo_api_function("fread") << + boost::errinfo_errno(errno) << + boost::errinfo_file_handle(f) ); + return nr; + }+
#include <boost/exception/errinfo_file_name.hpp>
+#include <boost/exception/error_info.hpp> +#include <string> + +namespace +boost + { + typedef error_info<struct errinfo_file_name_,std::string> errinfo_file_name; + }+
This type is designed to be used as a standard error_info instance for transporting a relevant file name in exceptions deriving from boost::exception.
+#include <boost/exception/errinfo_api_function.hpp> +#include <boost/exception/errinfo_at_line.hpp> +#include <boost/exception/errinfo_errno.hpp> +#include <boost/exception/errinfo_file_handle.hpp> +#include <boost/exception/errinfo_file_name.hpp> +#include <boost/exception/errinfo_file_open_mode.hpp> +#include <boost/exception/info.hpp> +#include <boost/throw_exception.hpp> +#include <boost/shared_ptr.hpp> +#include <boost/weak_ptr.hpp> +#include <stdio.h> +#include <errno.h> +#include <exception> + +struct error : virtual std::exception, virtual boost::exception { }; +struct file_error : virtual error { }; +struct file_open_error: virtual file_error { }; +struct file_read_error: virtual file_error { }; + +boost::shared_ptr<FILE> +open_file( char const * file, char const * mode ) + { + if( FILE * f=fopen(file,mode) ) + return boost::shared_ptr<FILE>(f,fclose); + else + BOOST_THROW_EXCEPTION( + file_open_error() << + boost::errinfo_api_function("fopen") << + boost::errinfo_errno(errno) << + boost::errinfo_file_name(file) << + boost::errinfo_file_open_mode(mode) ); + } + +size_t +read_file( boost::shared_ptr<FILE> const & f, void * buf, size_t size ) + { + size_t nr=fread(buf,1,size,f.get()); + if( ferror(f.get()) ) + BOOST_THROW_EXCEPTION( + file_read_error() << + boost::errinfo_api_function("fread") << + boost::errinfo_errno(errno) << + boost::errinfo_file_handle(f) ); + return nr; + }+
#include <boost/exception/errinfo_file_open_mode.hpp>
+#include <boost/exception/error_info.hpp> +#include <string> + +namespace +boost + { + typedef error_info<struct errinfo_file_open_mode_,std::string> errinfo_file_open_mode; + }+
This type is designed to be used as a standard error_info instance for transporting the string passed as a second argument to fopen in exceptions indicating fopen failures and deriving from boost::exception.
+#include <boost/exception/errinfo_api_function.hpp> +#include <boost/exception/errinfo_at_line.hpp> +#include <boost/exception/errinfo_errno.hpp> +#include <boost/exception/errinfo_file_handle.hpp> +#include <boost/exception/errinfo_file_name.hpp> +#include <boost/exception/errinfo_file_open_mode.hpp> +#include <boost/exception/info.hpp> +#include <boost/throw_exception.hpp> +#include <boost/shared_ptr.hpp> +#include <boost/weak_ptr.hpp> +#include <stdio.h> +#include <errno.h> +#include <exception> + +struct error : virtual std::exception, virtual boost::exception { }; +struct file_error : virtual error { }; +struct file_open_error: virtual file_error { }; +struct file_read_error: virtual file_error { }; + +boost::shared_ptr<FILE> +open_file( char const * file, char const * mode ) + { + if( FILE * f=fopen(file,mode) ) + return boost::shared_ptr<FILE>(f,fclose); + else + BOOST_THROW_EXCEPTION( + file_open_error() << + boost::errinfo_api_function("fopen") << + boost::errinfo_errno(errno) << + boost::errinfo_file_name(file) << + boost::errinfo_file_open_mode(mode) ); + } + +size_t +read_file( boost::shared_ptr<FILE> const & f, void * buf, size_t size ) + { + size_t nr=fread(buf,1,size,f.get()); + if( ferror(f.get()) ) + BOOST_THROW_EXCEPTION( + file_read_error() << + boost::errinfo_api_function("fread") << + boost::errinfo_errno(errno) << + boost::errinfo_file_handle(f) ); + return nr; + }+
#include <boost/exception/errinfo_type_info_name.hpp>
+#include <boost/exception/error_info.hpp> +#include <string> + +namespace +boost + { + typedef error_info<struct errinfo_type_info_name_,std::string> errinfo_type_info_name; + }+
This type is designed to be used as a standard error_info instance for transporting strings returned by std::type_info::name in exceptions deriving from boost::exception objects.
+For convenience and uniformity, Boost Exception defines the following commonly used error_info typedefs, ready for use with operator<<:
+This is a possible message it may display, the first line is only possible if BOOST_THROW_EXCEPTION is used:
-example_io.cpp(83): Throw in function void parse_file(const char *) -Dynamic exception type: class file_open_error +example_io.cpp(70): Throw in function class boost::shared_ptr<struct _iobuf> __cdecl my_fopen(const char *,const char *) +Dynamic exception type: class boost::exception_detail::clone_impl<class fopen_error> std::exception::what: example_io error -[struct tag_errno_code *] = 2, OS says "No such file or directory" -[struct tag_file_name *] = tmp1.xml -[struct tag_function *] = fopen -[struct tag_open_mode *] = rb+[struct boost::errinfo_api_function_ *] = fopen +[struct boost::errinfo_errno_ *] = 2, "No such file or directory" +[struct boost::errinfo_file_name_ *] = tmp1.txt +[struct boost::errinfo_file_open_mode_ *] = rb
The boost::throw_exception function predates the Boost Exception library and there has been some concern about its current behavior of injecting boost::exception as a base of any exception passed to boost::throw_exception. Such concerns are dictated by the typical strict interpretation of a common principle in C and C++, that users only pay for features they actually use.
The problem is that users of Boost Exception can't by themselves cause a library to throw types that derive from boost::exception, and without this they can't use any of the Boost Exception facilities.
diff --git a/doc/headers.html b/doc/headers.html index 99f97f1..2011fcf 100644 --- a/doc/headers.html +++ b/doc/headers.html @@ -21,11 +21,18 @@boost/exception/current_exception_cast.hpp
boost/exception/diagnostic_information.hpp
boost/exception/enable_current_exception.hpp
boost/exception/enable_error_info.hpp
+boost/exception/errinfo_api_function.hpp
+boost/exception/errinfo_at_line.hpp
+boost/exception/errinfo_errno.hpp
+boost/exception/errinfo_file_handle.hpp
+boost/exception/errinfo_file_name.hpp
+boost/exception/errinfo_file_open_mode.hpp
+boost/exception/errinfo_type_info_name.hpp
boost/exception/error_info.hpp
boost/exception/get_error_info.hpp
@@ -53,6 +60,13 @@#include <boost/exception/error_info.hpp>
namespace
@@ -172,14 +172,82 @@ boost
void throw_exception( E const & e );
#endif
}
#include <boost/exception.hpp>
+#include <boost/exception/errinfo_api_function.hpp>
+#include <boost/exception/error_info.hpp> + +namespace +boost + { + typedef error_info<struct errinfo_api_function_,int> errinfo_api_function; + }+
#include <boost/exception/errinfo_at_line.hpp>
+#include <boost/exception/error_info.hpp> + +namespace +boost + { + typedef error_info<struct errinfo_at_line_,int> errinfo_at_line; + }+
#include <boost/exception/errinfo_errno.hpp>
+#include <boost/exception/error_info.hpp> +#include <errno.h> + +namespace +boost + { + typedef error_info<struct errinfo_errno_,int> errinfo_errno; + }+
#include <boost/exception/errinfo_file_handle.hpp>
+#include <boost/exception/error_info.hpp> + +namespace +boost + { + template <class> class weak_ptr; + typedef error_info<struct errinfo_file_handle_,weak_ptr<FILE> > errinfo_file_handle; + }+
#include <boost/exception/errinfo_file_name.hpp>
+#include <boost/exception/error_info.hpp> +#include <string> + +namespace +boost + { + typedef error_info<struct errinfo_file_name_,std::string> errinfo_file_name; + }+
#include <boost/exception/errinfo_file_open_mode.hpp>
+#include <boost/exception/error_info.hpp> +#include <string> + +namespace +boost + { + typedef error_info<struct errinfo_file_open_mode_,std::string> errinfo_file_open_mode; + }+
#include <boost/exception/errinfo_type_info_name.hpp>
+#include <boost/exception/error_info.hpp> +#include <string> + +namespace +boost + { + typedef error_info<struct errinfo_type_info_name_,std::string> errinfo_type_info_name; + }+
#include <boost/exception/all.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/exception/error_info.hpp>
#include <boost/exception/exception.hpp>
#include <boost/exception/get_error_info.hpp>
#include <boost/exception/info.hpp>
#include <boost/exception/info_tuple.hpp>
-#include <boost/exception_ptr.hpp>
+#include <boost/exception_ptr.hpp>
+#include <boost/exception/errinfo_api_function.hpp>
+#include <boost/exception/errinfo_at_line.hpp>
+#include <boost/exception/errinfo_errno.hpp>
+#include <boost/exception/errinfo_file_handle.hpp>
+#include <boost/exception/errinfo_file_name.hpp>
+#include <boost/exception/errinfo_file_open_mode.hpp>
+#include <boost/exception/errinfo_type_info_name.hpp>
The returned string 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 <boost/exception/all.hpp> #include <iostream> void f(); //throws unknown types that derive from boost::exception. @@ -47,13 +47,13 @@ g() }Example:
this is a possible output from the diagnostic_information function, as used in libs/exception/example/example_io.cpp:
-example_io.cpp(83): Throw in function class boost::shared_ptr<struct _iobuf> __cdecl my_fopen(const char *,const char *) -Dynamic exception type: class boost::exception_detail::clone_impl<class fopen_error> +example_io.cpp(70): Throw in function class boost::shared_ptr<struct _iobuf> __cdecl my_fopen(const char *,const char *) +Dynamic exception type: class boost::exception_detail::clone_impl<struct fopen_error> std::exception::what: example_io error -[struct tag_errno *] = 2, OS says "No such file or directory" -[struct tag_file_name *] = tmp1.txt -[struct tag_function *] = fopen -[struct tag_open_mode *] = rb+[struct boost::errinfo_api_function_ *] = fopen +[struct boost::errinfo_errno_ *] = 2, "No such file or directory" +[struct boost::errinfo_file_name_ *] = tmp1.txt +[struct boost::errinfo_file_open_mode_ *] = rb
Some exception hierarchies can not be modified to make boost::exception a base type. In 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 <boost/exception/all.hpp> #include <stdexcept> typedef boost::error_info<struct tag_std_range_min,size_t> std_range_min; diff --git a/doc/tutorial_exception_ptr.html b/doc/tutorial_exception_ptr.html index 85fe37e..4cc9ef9 100644 --- a/doc/tutorial_exception_ptr.html +++ b/doc/tutorial_exception_ptr.html @@ -28,19 +28,18 @@
Here is how cloning can be enabled in a throw-expression (15.1):
#include <boost/exception/info.hpp> +#include <boost/exception/errinfo_errno.hpp> #include <stdio.h> #include <errno.h> -typedef boost::error_info<struct tag_errno,int> errno_info; - -class file_read_error: public boost::exception { }; +struct file_read_error: virtual 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); + boost::errinfo_errno(errno); }
Of course, enable_current_exception may be used with any exception type; there is no requirement that it should derive from boost::exception.
The following example demonstrates how errno can be stored in exception objects using Boost Exception:
-#include <boost/exception.hpp> -#include <errno.h> ++#include <boost/exception/all.hpp> #include <iostream> -typedef boost::error_info<struct tag_errno,int> errno_info; //(1) +typedef boost::error_info<struct tag_my_info,int> my_info; //(1) -class my_error: public boost::exception, public std::exception { }; //(2) +struct my_error: virtual boost::exception, virtual 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.
+ throw my_error() << my_info(42); //(3) + }
First, we instantiate the error_info template using a unique identifier -- tag_my_info, 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:
+The stored my_info value can be recovered at a later time like this:
// ...continued void @@ -59,8 +57,8 @@ g() catch( my_error & x ) { - if( int const * err=boost::get_error_info<errno_info>(x) ) - std::cerr << "Error code: " << *err; + if( int const * mi=boost::get_error_info<my_info>(x) ) + std::cerr << "My info: " << *mi; } }
The get_error_info function template is instantiated with the typedef from (1), and is passed an exception object of a polymorphic type. If the exception object contains the requested value, err will point to it; otherwise a null pointer is returned.
@@ -75,28 +73,25 @@ file_read( FILE * f, void * buffer, size_t size ) }How can the file_read function pass a file name to the exception type constructor? All it has is a FILE handle.
Using boost::exception allows us to free the file_read function from the burden of storing the file name in exceptions it throws:
-#include <boost/exception.hpp> +#include <boost/exception/all.hpp> +#include <boost/shared_ptr.hpp> #include <stdio.h> #include <errno.h> -typedef boost::error_info<struct tag_errno,int> errno_info; - -class file_read_error: public boost::exception { }; +struct file_read_error: virtual 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); + throw file_read_error() << boost::errinfo_errno(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/exception/all.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 ); @@ -113,7 +108,7 @@ parse_file( char const * file_name ) catch( boost::exception & e ) { - e << file_name_info(file_name); + e << boost::errinfo_file_name(file_name); throw; } }@@ -122,17 +117,17 @@ parse_file( char const * file_name )
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/exception/errinfo_file_name.hpp> +#include <boost/exception/errinfo_api_function.hpp> +#include <boost/exception/errinfo_errno.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; +typedef boost::tuple<boost::errinfo_api_function,boost::errinfo_errno> clib_failure; -class file_open_error: public boost::exception { }; +struct file_open_error: virtual boost::exception { }; boost::shared_ptr<FILE> file_open( char const * name, char const * mode ) @@ -141,7 +136,7 @@ file_open( char const * name, char const * mode ) return boost::shared_ptr<FILE>(f,fclose); else throw file_open_error() << - file_name_info(name) << + boost::errinfo_file_name(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.
diff --git a/doc/types.html b/doc/types.html index 9505d28..630fa32 100644 --- a/doc/types.html +++ b/doc/types.html @@ -22,6 +22,13 @@