|
Boost : |
From: Jeremy Day (jeremy.day_at_[hidden])
Date: 2006-07-18 13:06:34
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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk