CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #128

Locals Before and After the Throw

hardExceptions

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <stdexcept>
3#include <string>
4 
5class Note {
6 std::string m_text;
7public:
8 explicit Note(const std::string& text) : m_text(text) {
9 std::cout << "note: " << m_text << "\n";
10 }
11 ~Note() { std::cout << "torn up: " << m_text << "\n"; }
12};
13 
14int main() {
15 try {
16 Note first("buy milk");
17 throw std::runtime_error("out of paper");
18 Note second("call mum");
19 std::cout << "both notes written\n";
20 } catch (const std::runtime_error& e) {
21 std::cout << "caught: " << e.what() << "\n";
22 }
23 std::cout << "end\n";
24 return 0;
25}

🎯 Your Answer