|
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-27 15:51:23
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.)
-- 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