Boost logo

Boost :

From: Oleg Abrosimov (beholder_at_[hidden])
Date: 2006-07-27 01:03:10


Jeremy Day wrote:
> [snip]
> Now if ExceptionHandler has a nested exceptions type you don't need to do
> something along the lines of
> catcher<my_exceptions>(my_exception_handler()). However, if you want to
> avoid coupling the exceptions with the handler you can have them separate.
>
> Here is the full example from General_Catcher.cpp.
>
> #include "catcher.hpp"
> #include <iostream>
> #include <boost/mpl/vector.hpp>
>
> [snip]
>
> int main(int argc, char* argv[])
> {
> typedef boost::mpl::vector<std::logic_error, std::exception> exceptions;
>
> try
> {
> throw(std::logic_error("testing"));
> } //end of try block
> catch(...)
> {
> catcher<exceptions>(my_exception_handler2());
> } //end of catch(...) block
> [snip]
> } //end of int main(int argc, char* argv[])

Hello, Jeremy!

Thank you for your involvement in this topic. I'm very interested in
pushing it to a complete solution one day.

one further improvement I see is to define simple macros to eliminate
some details from client code. something like this would be appropriate:

try {
     throw std::logic_error("testing");
} BOOST_CATCH(my_exception_handler());

and

try {
     throw std::logic_error("testing");
} BOOST_CATCH_EX(exceptions, my_exception_handler());

and of course, I agree with David Abrahams that exceptions typelist
should be sorted to handle most derived first. boost::is_base_of<Base,
Derived> from type_traits library can be used to achieve it.

One more idea to improve maintainability of client code:

class my_exceptions_handler
{
     BOOST_DEFINE_EXCEPTION_HANDLER_FIRST(exception_type,
exception_variable, 1)
     {
         //code here
     }
     BOOST_DEFINE_EXCEPTION_HANDLER(exception_type2, exception_variable,
1, 2)
     {
         //code here
     }
     BOOST_DEFINE_EXCEPTION_HANDLER(exception_type2, exception_variable,
2, 3)
     {
         //code here
     }
     BOOST_DEFINE_EXCEPTION_HANDLER_LAST(exception_type,
exception_variable, 3)
     {
         //code here
     }
};

These macros automatically define exceptions list eliminating last piece
of code duplication. numbers are used to decorate typelist' name that is
already defined.

Best,
Oleg Abrosimov.


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