← Quiz: Exception Handling in C++: all 10 questions
Quiz: Exception Handling in C++ · Question 5 of 10
What does this print? (code)
What does this print?
#include <iostream>
#include <stdexcept>
using namespace std;
struct G {
char c;
G(char x) : c(x) {}
~G() { cout << c; }
};
void f() {
G a('1');
G b('2');
throw runtime_error("e");
}
int main() {
try { f(); } catch (...) { cout << "C"; }
return 0;
}
- A.12C
- B.21C
- C.C21
- D.C
Answer & explanation
Correct answer: 21C
During unwinding, b is destroyed before a (reverse order of creation), and then the catch block runs.
Can you get the rest right without peeking?
Take the timed Quiz: Exception Handling in C++ quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →