- 一、异常传播
- 二、重抛异常
一、异常传播
#includeusing std::cout; using std::endl; class Exception1 {}; class Exception2{}; class Exception3{}; void f1(); void f2(); void f3(); int main() { try { f1(); cout << "mainn"; } catch (const std::exception& e) { cout << "catch f1()n"; } } void f1() { try { f2(); cout << "f1n"; } catch (const Exception1& e) { cout << "catch f2()n"; } } void f2() { try { f3(); cout << "f2n"; } catch (const Exception2& e) { cout << "catch f3()n"; } } void f3() { throw Exception2(); }
结果如上图所示
二、重抛异常#include#include #include using std::cout; using std::endl; void f(); int main() { try { f(); } catch (const std::exception& e) { cout << "catched exception : " << e.what() << endl; } } void f() { try { throw std::logic_error("Throw in f()"); } catch (const std::exception& e) { cout << "catched in f()" << endl; throw; } }
结果如上图所示。



