Boost logo

Boost :

From: Jeremy Day (jeremy.day_at_[hidden])
Date: 2006-07-26 22:53:35


All,

Sorry for the rather long delay. I've been meaning to get back to this, but
I haven't had time until now. I just uploaded a new version of the catcher
to the Vault. You can find it here: http://tinyurl.com/hrdm6 .

On 7/20/06, Oleg Abrosimov <beholder_at_[hidden]> wrote:
>
> int main()
> try {
> throw std::logic_error("testing");
> } catch(...) {
> catcher<exceptions>(my_exception_handler()); //Note here
> }
>
> with this transformation one can use one my_exception_handler class with
> many reduced exceptions typelists. It also eliminates the duplication
> problem. Duplication still exists but is intensional.

I spent a few minutes experimenting. Before I just had this little
function:

template <typename ExceptionHandler>
void catcher(ExceptionHandler& oHandler)
    {
    catcher_while_<ExceptionHandler::exceptions>(oHandler);
    }

Per Oleg's suggestion I added an additional function, which looks like this:

template <typename Exceptions, typename ExceptionHandler>
void catcher(ExceptionHandler& oHandler)
    {
    catcher_while_<Exceptions>(oHandler);
    }

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>

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;
        } //end of void operator()(std::exception& oError)

    void operator()(std::logic_error& oError)
        {
        std::cout << "my_exception_handler::operator()(std::logic_error&)"
<< std::endl;
        } //end of void operator()(std::logic_error& oError)

    void operator()(void)
        {
        std::cout << "my_exception_handler::operator()(void)" << std::endl;
        } //end of void operator()(void)
    }; //end of struct my_exception_handler

struct my_exception_handler2
    {
    void operator()(std::exception& oError)
        {
        std::cout << "my_exception_handler2::operator()(std::exception&)" <<
std::endl;
        } //end of void operator()(std::exception& oError)

    void operator()(std::logic_error& oError)
        {
        std::cout << "my_exception_handler2::operator()(std::logic_error&)"
<< std::endl;
        } //end of void operator()(std::logic_error& oError)

    void operator()(void)
        {
        std::cout << "my_exception_handler2::operator()(void)" << std::endl;
        } //end of void operator()(void)
    }; //end of struct my_exception_handler

int main(int argc, char* argv[])
    {
    try
        {
        throw(std::logic_error("testing"));
        } //end of try block
    catch(...)
        {
        catcher(my_exception_handler());
        } //end of catch(...) block

    try
        {
        throw(std::exception("testing"));
        } //end of try block
    catch(...)
        {
        catcher(my_exception_handler());
        } //end of catch(...) block

    try
        {
        throw("testing");
        } //end of try block
    catch(...)
        {
        catcher(my_exception_handler());
        } //end of catch(...) block

    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

    try
        {
        throw(std::exception("testing"));
        } //end of try block
    catch(...)
        {
        catcher<exceptions>(my_exception_handler2());
        } //end of catch(...) block

    try
        {
        throw("testing");
        } //end of try block
    catch(...)
        {
        catcher<exceptions>(my_exception_handler2());
        } //end of catch(...) block

    return(0);
    } //end of int main(int argc, char* argv[])


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