Boost logo

Boost Users :

Subject: Re: [Boost-users] [Boost.Exception] A couple of unspotted areas
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2012-07-19 18:14:31


On Thu, Jul 19, 2012 at 6:56 AM, Alexander Mingalev <infest21h_at_[hidden]>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



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net