Practice mode C++

Assuming standard headers and using namespace std, what does this C++ code print? struct G { ~G() { cout << “G”; } }; int main() { try { G g; throw 1; cout << “T”; } catch (int) { cout << “C”; } }

Correct answer A. GC

Explanation

When throw executes, control leaves the try block immediately, so T is never printed. During stack unwinding every fully constructed automatic object in the exited scope is destroyed, so G's destructor prints G before the handler runs and prints C. This guaranteed cleanup during unwinding is the foundation of RAII-based resource safety.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.