Is it possible to catch an exception of lambda type?

Exception handlers are matched based on type, and the implicit conversions done to match an exception object to a handler are more limited than in other contexts.

Each lambda expression introduces a closure type that is unique to the surrounding scope. So your naive attempt cannot work, for []{} has an entirely different type in the throw expression and the handler!

But you are correct. C++ allows you to throw any object. So if you explicitly convert the lambda before-hand to a type that matches an exception handler, it will allow you to call that arbitrary callable. For instance:

try {
    throw std::function<void()>{ []{} }; // Note the explicit conversion
} catch(std::function<void()> const& f) {
    f();
}

This may have interesting utility, but I’d caution against throwing things not derived from std::exception. A better option would probably be to create a type that derives from std::exception and can hold a callable.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)