// ============================================================================ // p1c07_04 — 7.2.3 栈解旋(unwinding) // 来源:川大 C++/LinuxC wiki「02.Qt方向」 // 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用 // // --- 原文片段 [pageId:blockIdx] --- 标注出处。 // 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。 // ============================================================================ #include using namespace std; #include static inline void pause() { printf("按回车键继续..."); (void)getchar(); } // --- 原文片段 [58954474:3] 7.2.3 栈解旋(unwinding) --- class Person{ public: Person(string name){ mName = name; cout << mName << "对象被创建!" << endl; } ~Person(){ cout << mName << "对象被析构!" << endl; } public: string mName; }; void TestFunction(){ Person p1("aaa"); Person p2("bbb"); Person p3("ccc"); //抛出异常 throw 10; } int main(){ try{ TestFunction(); } catch (...){ cout << "异常被捕获!" << endl; } pause(); return EXIT_SUCCESS; } // --- main ---