|
Boost : |
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2006-07-18 13:42:10
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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk