
"Andrey Semashev" <andysem@mail.ru> writes: Andrey, your mailer wraps long lines; you might want to put your code in an attachment next time. http://www.boost.org/more/discussion_policy.htm#longlines
struct my_exception : public std::exception { /* ... */ };
void guarded_foo(int n, const char* p); // throws bad_cast, bad_alloc and my_exception
// This is the class of the exception handler object struct CHandler { void on_exception(std::bad_cast& e) throw() { std::cout << "on_exception called, bad_cast caught" << std::endl; } void on_exception(std::bad_alloc& e) throw() { std::cout << "on_exception called, bad_alloc caught" << std::endl; } void on_exception(my_exception& e) throw() { std::cout << "on_exception called, my_exception caught" << std::endl; } };
void foo(int n) { leaving_scope_guard< guard::with_catcher< // Indicate that we're expecting some // exceptions to be thrown by guarded_foo CHandler, // This is the class, which object is to // process exceptions caught guard::exceptions< std::bad_cast, std::bad_alloc, my_exception >, // Exceptions to be caught (up to // 10 exception types supported)
This part worries me thes reasons: 0. I doubt its utility. 1. It induces a catch block and in many cases a catch block should be avoided when possible in exception-neutral code. 2. I think it may encourage abuse with the same lure that exception-specifications present to the casual user.
boost::shared_ptr< CHandler > // An optional parameter // describes the way the // pointer to CHandler // object should be stored. // The default value would // be CHandler*.
Do we really need all this parameterization? I think a generalized scopeguard is a good idea, but this smells like overkill.
> > guard(boost::bind(&guarded_foo, n, "called from foo"), boost::shared_ptr< CHandler >(new CHandler)); // Guard object
// ... }
-- Dave Abrahams Boost Consulting www.boost-consulting.com