[exception, system] using boost::system::error_code with boost::exception

Hi boost users, I'm a very happy user of both error_code and exception, but now I found a new use case which is to combine them. So I'm looking for an example of how to add an error_code into an exception and get it all nicely formatted with diagnostic_information. I can see how to use error_info [1] but not error_code. Any pointers would be appreciated. Cheers Marco -- So young, and already so unknown -- Pauli blog: http://mcraveiro.blogspot.com [1] http://www.boost.org/doc/libs/1_52_0/libs/exception/doc/tutorial_transportin...

On Mon, Dec 10, 2012 at 3:37 AM, Marco Craveiro <marco.craveiro@gmail.com> wrote:
Hi boost users,
I'm a very happy user of both error_code and exception, but now I found a new use case which is to combine them. So I'm looking for an example of how to add an error_code into an exception and get it all nicely formatted with diagnostic_information. I can see how to use error_info [1] but not error_code. Any pointers would be appreciated.
#include <boost/exception/info.hpp> typedef boost::error_info<struct tag_error_code,error_code> error_code_; With this, you can store error_code_ into any exceptions deriving from boost::exception. You can then overload to_string() and use the observers of boost::error_code to define how it appears in the string returned by boost::diagnostic_information: namespace boost { namespace system { std::string to_string( error_code const & ec ) { return .....; } } } Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
participants (2)
-
Emil Dotchevski
-
Marco Craveiro