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.
Answer checked How we write and check questions
Discussion