Boost Exception

error_info

#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 & v );    
        value_type const & value() const;
        value_type & value();    
        };
    }

Requirements:

T must have accessible copy constructor and must not be a reference (there is no requirement that T's copy constructor does not throw.)

Description:

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.

Usage:

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, for example:

#include <boost/exception/error_info.hpp>

struct tag_errno;
typedef boost::error_info<tag_errno,int> errno_info;

Or, the shorter equivalent:

#include <boost/exception/error_info.hpp>

typedef boost::error_info<struct tag_errno,int> errno_info;

This errno_info typedef can be passed to operator<< (#include <boost/exception/info.hpp> first) to store an int named tag_errno in exceptions of types that derive from boost::exception:

throw file_read_error() << errno_info(errno);

It can also be passed to get_error_info (#include <boost/exception/get_error_info.hpp> first) to retrieve the tag_errno int from a boost::exception:

catch( boost::exception & x )
    {
    if( int const * e=boost::get_error_info<errno_info>(x) )
        ....
    }

For convenience and uniformity, Boost Exception defines the following commonly used error_info typedefs, ready for use with operator<<: