Boost logo

Boost Users :

Subject: Re: [Boost-users] [Exception] How do I inherit custom exception hierarchy from std::runtime_error rather than from std::exception?
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2013-11-28 02:31:10


On Wed, Nov 27, 2013 at 10:39 PM, Eric Prud'hommeaux <eric_at_[hidden]> wrote:
> On Nov 27, 2013 9:52 PM, "Emil Dotchevski" <emildotchevski_at_[hidden]> wrote:
>>
>> On Wed, Nov 27, 2013 at 8:19 AM, Egor Tensin <egor.tensin_at_[hidden]>
>> wrote:
>> > The issue that's confusing me arose from my intention of inheriting my
>> > exception classes from std::runtime_error rather than from
>> > std::exception. It seemed logical to me, as my exceptions are really
>> > runtime errors; getting something meaningful via what() also seemed
>> > like a good idea. However, this doesn't compile:
>>
>> <cut>
>>
>> > struct foo_error : virtual boost::exception, virtual std::runtime_error
>> > {
>> > explicit foo_error(const char *const what)
>> > : std::runtime_error(what)
>> > { }
>>
>> Yes, you'd have to use non-virtual inheritance if you want to be able
>> to pass the what parameter like this. However, I would recommend
>> deriving virtually from std::exception and using different exception
>> types rather than different what strings for different failures. It is
>> usually appropriate to override what() and implement it in terms of
>> diagnostic_information_what (see
>>
>> www.boost.org/doc/libs/release/libs/exception/doc/diagnostic_information_what.html.)
>
> Your advice makes sense to me but I'm curious what semantics you write off,
> either explicit in the runtime_error definition or implied by library or
> development tool interpretation of runtime_error.

I've never seen anyone catch a std::runtime_error for any other reason
than to print the what() string; and std::exception is just as good
for that.

The other thing is, virtual inheritance is used in exception class
hierarchies to enable safe use of multiple inheritance, but
std::runtime_error doesn't derive virtually from std::exception. This
makes std::runtime_error and virtual inheritance mutually exclusive in
my book.

-- 
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