[Boost.Exception] A couple of unspotted areas

Hi, I'm trying to use Boost.Exception library and I have some issues here. 1. How to build exception hierarchy correctly? According to examples I wrote: #include <iostream> #include <boost/exception/all.hpp> struct Error1 : virtual public std::exception, virtual public boost::exception { Error1(const char* msg) : std::exception(msg) {} }; struct Error2 : virtual public Error1 { Error2(const char* msg) : Error1(msg) {} }; ... std::cout << Error2("zzz").what() << std::endl; I've got "Unknown exception" instead of "zzz" 2. Strange behavior of boost::enable_error_info Look at this example: #include <iostream> #include <boost/exception/all.hpp> typedef boost::error_info<struct Tag1_, int> Tag1; typedef boost::error_info<struct Tag2_, int> Tag2; void f1(); void f2(); int main() { try { f1(); } catch (const std::exception& e) { std::cout << boost::diagnostic_information(e) << std::endl; } return 0; } void f1() { try { f2(); } catch (const std::exception& e) { throw boost::enable_error_info(e) << Tag1(1); } } void f2() { throw boost::enable_error_info(std::runtime_error("f2 exception")) << Tag2(2) ; } It prints: Throw location unknown (consider using BOOST_THROW_EXCEPTION) Dynamic exception type: boost::exception_detail::error_info_injector<std::exception> std::exception::what: std::exception [Tag1_*] = 1 Where is Tag2 data? Looks like enable_error_info erases all previous accumulated infos. Besides, type of exception is reported as "std::exception", not "std::runtime_error"

On 19/07/2012 10:56 a.m., Alexander Mingalev wrote:
catch (const std::exception& e) { throw boost::enable_error_info(e) << Tag1(1); }
I believe that should be catch(boost::exception& e) { e << Tag1(1); throw; // rethrow original exception } so to preserve the original exception being thrown. You are currently throwing a new exception copied from std::exception e. Agustín K-ballo Bergé.- http://fusionfenix.com

On Thu, Jul 19, 2012 at 6:56 AM, Alexander Mingalev <infest21h@gmail.com>wrote:
1. How to build exception hierarchy correctly?
According to examples I wrote:
#include <iostream> #include <boost/exception/all.hpp>
struct Error1 : virtual public std::exception, virtual public boost::exception { Error1(const char* msg) : std::exception(msg) {} };
struct Error2 : virtual public Error1 { Error2(const char* msg) : Error1(msg) {} };
...
std::cout << Error2("zzz").what() << std::endl;
I'd recommend leaving the std::what() alone. Define the exception type hierarchy as simple empty structs: struct base_exception: virtual std::exception, virtual boost::exception { }; struct error1: virtual base_exception { }; struct error2: virtual error1 { }; struct error3: virtual error1, virtual error2 { }; Regardless of their type, exception objects can carry any number of boost::error_info objects.
2. Strange behavior of boost::enable_error_info <snipped>
Where is Tag2 data?
Look at Augustín's answer. I'd also recommend to not bother with enable_error_info, just call BOOST_THROW_EXCEPTION to throw objects of "empty" types arranged in a hierarchy deriving from boost::exception. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
participants (3)
-
Agustín K-ballo Bergé
-
Alexander Mingalev
-
Emil Dotchevski