Boost logo

Boost :

From: AlisdairM (alisdair.meredith_at_[hidden])
Date: 2006-07-18 15:30:10


Jeremy Day wrote:

> > Personally I would prefer it if handlers automatically got the same
> > order as would be provided by overload resolution. It's almost
> > always a mistake to write
> >
> > catch(Base&) { ... }
> > catch(Derived&) { ... }
>
>
> Surely it would be possible to reorder the type list that catcher uses
> (which it gets from the exception handling class, such as
> my_exception_handler) so that the order is always from most derived
> (such as Derived&) to least derived (such as Base&). Is there an MPL
> algorithm somewhere that will do that? I feel certain that I saw
> some code by Andrei Alexandrescu that did something along those lines.

Note the word 'almost' in Dave's statement ;?)

No, I don't know any GOOD reason to write catch clauses in Base/Derived
order, but I can write code that will be caught by the derived handler:

struct my_exception : std::runtime_error, std::logic_error {
...
};

void test() {
  try {
    throw my_exception();
  }
  catch( const std::exception & ) {}
  catch( const std::runtime_error & ) {}
}

Now the question is - can I find a good example with an exception
hierarchy designed in this way, where catching on the derived type
first would break code? In priciple I can fake up the example, but how
likely is it to be a problem in real-world use?

-- 
AlisdairM

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk