All destructors for objects constructed in a block are called before an exception is thrown from that block. - C++ Statement

C++ examples for Statement:throw

Description

All destructors for objects constructed in a block are called before an exception is thrown from that block.

Demo Code

#include <iostream>
#include <stdexcept>

class C1 {//from w  w w.  j a v a  2s.c  om
 public:
    C1() { std::cout << "C1 constructor" << std::endl; }
    ~C1() { std::cout << "C1 destructor" << std::endl; }
};
class C2 {
 public:
    C2() { std::cout << "C2 constructor" << std::endl; }
    ~C2() { std::cout << "C2 destructor" << std::endl; }
};

int main(int argc, const char *argv[]) {
    try {
        C1 c1;
        C2 c2;

        throw std::exception();
    } catch (std::exception &e) {
        std::cout << "exception: " << e.what() << std::endl;
    }

    return 0;
}

Result


Related Tutorials