|
Boost : |
From: Jeremy Day (jeremy.day_at_[hidden])
Date: 2006-07-18 13:52:16
The exceptions will be caught in the order of the items in the exception
handler's nested type argument. So in the example catcher tries
std::logic_error first, and if the exception isn't of that type it tries
std::exception. If the exception isn't of that type it defaults to
operator()(void). So basically you take the order of the catch block (top
to bottom) and "rotate" it so that the exception types appear in the list
from left to right.
Jeremy
On 7/18/06, Emil Dotchevski <emildotchevski_at_[hidden]> wrote:
>
> You seem to be replacing the sequence of catch-es (which is
> order-specific),
> with overloading (which isn't). The order is important, because in a way
> each catch is a more generic fallback for the previous ones.
>
> --Emil
>
> Jeremy Day wrote:
> > All,
> >
> > A message a few days ago, along with my current reading of the book
> > 'C++ Template Metaprogramming', piqued my interest in the idea of an
> > "exception visitor". I put together a little something, and I'm
> > wondering if people might be interested in my (rather poor, at the
> > moment) code.
> >
> > Problem:
> >
> > When using exceptions it is very easy to end up with code that looks
> > something like this:
> >
> > try
> > {
> > //Some code that might throw exceptions.
> > }
> > catch(std::logic_error&)
> > {
> > //Handle the exception.
> > }
> > catch(std::exception&)
> > {
> > //Handle exception.
> > }
> > catch(...)
> > {
> > //Handle everything else
> > }
> >
> > With my code you can transform the above into something along these
> > lines:
> >
> > struct my_exception_handler
> > {
> > typedef boost::mpl::vector<std::logic_error, std::exception>
> > exceptions;
> >
> > void operator()(std::exception& oError)
> > {
> > std::cout <<
> > "my_exception_handler::operator()(std::exception&)" << std::endl;
> > }
> >
> > void operator()(std::logic_error& oError)
> > {
> > std::cout <<
> > "my_exception_handler::operator()(std::logic_error&)" << std::endl;
> > }
> >
> > void operator()(void)
> > {
> > std::cout << "my_exception_handler::operator()(void)" <<
> > std::endl; }
> > };
> >
> > //Application code.
> >
> > try
> > {
> > //Some code that might throw exceptions, such as:
> > throw std::logic_error("A sample logic error.");
> > }
> > catch(...)
> > {
> > //Magic happens here.
> > catcher(my_exception_handler()); //This prints out
> > "my_exception_handler::operator()(std::logic_error&)"
> > }
> >
> > If anyone is interested I will post what I have for critique (I'm a
> > relative newbie with MPL, so I will likely need a lot of help). I
> > look forward to
> > any and all comments.
> >
> > Jeremy
> > _______________________________________________
> > Unsubscribe & other changes:
> > http://lists.boost.org/mailman/listinfo.cgi/boost
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk