// RAII: Resource Acquisition Is Initialization // - Tie resource management to object lifetime. // - ifstream does it well. // - Who frees p’s memory if an error is thrown? #include // for cout, cerr #include // for ifstream #include // for unique_ptr using namespace std; void bar() { ifstream in("/etc/resolv.conf"); double *p = new double; // unique_ptr p(new double); string s; if (getline(in, s) && s != "FIRST") throw "I am unhappy"s; // oops--forgot to delete p cout << "The first line is " << s << '\n'; delete p; } void foo() { // Having foo() call bar() reminds the students bar(); // that exceptions propagate through function calls } int main() { try { foo(); } catch (const string &msg) { cerr << "Caught an error: " << msg << "\n"; } return 0; // optional }