[C++] stack unwinding
Updated:
설명
- 예외가 발생하여 중간에 함수를 빠져나가도 해당 예외를 처리할 수 있는 catch 문을 만나면 그 사이의 있는 스택 정보가 자동으로 순서대로 정리
예제
- 코드
-
#include <iostream> #include <string> using namespace std; class AAA { private: const string strName; public: AAA(const string& strName) : strName(strName) { cout << "AAA() call - " << this->strName << endl; } ~AAA() { cout << "~AAA() call - " << this->strName << endl; } }; void func1(); void func2(); void func3(); void func1() { AAA aaa("func1"); func2(); } void func2() { AAA aaa("func2"); func3(); } void func3() { AAA aaa("func3"); throw 10; } int main() { try { func1(); } catch (int& iEx) { cout << "main catch int" << endl; } return 0; }
-
- 실행 결과
-
AAA() call - func1 AAA() call - func2 AAA() call - func3 ~AAA() call - func3 ~AAA() call - func2 ~AAA() call - func1 main catch int
-