Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class for handling custom exception

Tags:

c++

c++11

I would like to create a class which takes std::function and allow to handle specified exceptions but I'm not sure if it is possible.

Here is a pseudo draft:

//exception types
template<class... Args>
class CustomExceptionHandler
{
public:
    CustomExceptionHandler(std::function<void()> clb): clb_(std::move(clb)){}

    void ExecuteCallback()
    {
        try
        {
            clb_();
        }
        /*catch specified exception types*/
    }

private:
    std::function<void()> clb_;
};

//usage
CustomExceptionHandler<std::out_of_range, std::overflow_error> handler(clb);
handler.ExecuteCallback();

I don't know how to use a variadic template to grab exception types and use it later. Is it possible?

I guess that tuple may be helpful.

like image 297
Irbis Avatar asked Feb 21 '19 20:02

Irbis


People also ask

Which class is used for exception handling?

Exception Hierarchy The exception class is a subclass of the Throwable class.

What is the name of the custom exception class?

To create a custom exception, we have to extend the java. lang. Exception class. Note that we also have to provide a constructor that takes a String as the error message and called the parent class constructor.

What is custom exception handling in Java?

In Java, we can create our own exceptions that are derived classes of the Exception class. Creating our own Exception is known as custom exception or user-defined exception. Basically, Java custom exceptions are used to customize the exception according to user need.


1 Answers

It's possible! I've made a solution (which you can run here) that expands the parameter pack of exception types into a series of recursive function calls, where each function attempts to catch one type of exception. The innermost recursive call then invokes the callback.

namespace detail {    
    template<typename First>
    void catcher(std::function<void()>& clb){
        try {
            clb(); // invoke the callback directly
        } catch (const First& e){
            // TODO: handle error as needed
            std::cout << "Caught an exception with type \"" << typeid(e).name();
            std::cout << "\" and message \"" << e.what() << "\"\n";
        }
    }
    
    template<typename First, typename Second, typename... Rest>
    void catcher(std::function<void()>& clb){
        try {
            catcher<Second, Rest...>(clb); // invoke the callback inside of other handlers
        } catch (const First& e){
            // TODO: handle error as needed
            std::cout << "Caught an exception with type \"" << typeid(e).name();
            std::cout << "\" and message \"" << e.what() << "\"\n";
        }
    }
}

template<class... Args>
class CustomExceptionHandler
{
public:
    CustomExceptionHandler(std::function<void()> clb): clb_(std::move(clb)){}

    void ExecuteCallback()
    {
        detail::catcher<Args...>(clb_);
    }

private:
    std::function<void()> clb_;
};

int main(){
    
    std::function<void()> clb = [](){
        std::cout << "I'm gonna barf!\n";
        throw std::out_of_range("Yuck");
        //throw std::overflow_error("Ewww");
    };
    
    CustomExceptionHandler<std::out_of_range, std::overflow_error> handler(clb);
    handler.ExecuteCallback();
    
    return 0;
}

Output:

I'm gonna barf!

Caught an exception with type "St12out_of_range" and message "Yuck"

like image 56
alter_igel Avatar answered Oct 19 '22 20:10

alter_igel