Are C ++ `try` /` catch` blocks the same as other blocks with respect to RAII?

OK, so if I use the RAII idiom to control some context attribute *, will it work as I expect if I use it at the beginning of the block try?

In other words, if I have this:

struct raii {
    raii() {
        std::cout << "Scope init"
                  << std::endl; }
    ~raii() {
        std::cout << "Scope exit"
                  << std::endl; }
};

... and I successfully use it as follows:

{
    raii do_the_raii_thing;
    stuff_expecting_raii_context();
    /* … */
}

... will an RAII instance work in the same way if I do this:

try {
    raii do_the_raii_thing;
    stuff_expecting_raii_context_that_might_throw();
    /* … */
} catch (std::exception const&) {
    /* … */
}

This is probably a stupid question, but I want to check my own sanity about this - I do not clearly understand the intricacies of guarantees noexceptand other exceptions related to exceptions - so I apologize for my naive


[*] , Python C-APIs GIL ( ), RAII,

+4
3

"... RAII , :..."

. RAII , catch, .

, , throw, try/catch. .

+3

, , : RAII, .

+5

Yes, this is indicated in the Standard :

15.2 Constructors and destructors [except.ctor]

2 The destructor is called for each automatic object of the class type built using the block try. Automatic objects are destroyed in the reverse order of completion of their construction.

+2
source

Source: https://habr.com/ru/post/1625180/


All Articles