Apr 12, 2016

[reddit] [c++] List of ScopeGuard

[reddit] C++ List of ScopeGuard
[GotW.ca: Herb Sutter] Uncaught Exceptions
[cppref] std::uncaught_exceptions
[GSL] final_act allows you to ensure something gets run at the end of a scope

 
#include <iostream>
#include <exception>
#include <stdexcept>
 
struct Foo {
    ~Foo() {
        if (std::uncaught_exception()) {
            std::cout << "~Foo() called during stack unwinding\n";
        } else {
            std::cout << "~Foo() called normally\n";
        }
    }
};
int main()
{
    Foo f;
    try {
        Foo f;
        std::cout << "Exception thrown\n";
        throw std::runtime_error("test exception");
    } catch (const std::exception& e) {
        std::cout << "Exception caught: " << e.what() << '\n';
    }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.