Noah Roberts wrote:
[ ... ]
Figured. I still never quite get MI. Sometimes I really wish C++
did the interface or protocol thing.
MI is simply a generalized form -- it lets you do interfaces and
protocols, and a number of other things as well when needed.
How would I set this kind of situation up then? I want all of these
exceptions to be considered as ParentExceptions, which needs to look
like an exception, but I also want to qualify some of them to say,
"Well this is a range_error, this is an xxx_error...That way the
catch can be either specific to the class, or more general in terms
of stdlib.
At least at first blush, this sounds like you want std::exception as
the ultimate base class. You'll then derive your ParentException from
that, and the others from there.
I'm not entirely sure I've correctly interpreted what you've said
though.
Exceptions are _rarely_ a good time to use MI -- exceptions generally
seem to work best as a big more or less monolithic hierarchy.
I can concieve of one possibility though: assume you have a big program
in an existing framework (e.g. MFC) that already has its own exception
handling, its own exception hierarchy, etc. You want to migrate to the
std::exception hierarchy without doing wholesale modifications on your
large body of working code. At the same time, you need to be able to
throw an exception that can be caught by an existing handler that
expects something derived from CExecption, or by a new handler that
expects something derived from std::exception.
In this case, it might be reasonable to build a shadow hierarchy (so to
speek) that unifies the two. An example might be something like:
class you::range_error : public std::range_error, public
CRangeException {
// ...
};
and if your code throws one of these, it can be caught and viewed as
either an std::range_error, OR a CRangeException, whichever sort of
handler is found first.
This, however, looks to be like it has the potential for getting ugly
pretty quickly, and I hasten to point out that I'm _not_ particularly
recommending it as a good technique -- just pointing out a situation
where I can imagine that multiple inheritance and exceptions _might_
mix in a way that isn't immediately problematic.