栈解旋
异常被抛出后,从进入try块起,到异常被抛掷前,这期间在栈上的构造的
所有对象,都会被自动析构。析构的顺序与构造的顺序相反。这一过程称为栈
的解旋(unwinding)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #define _CRT_SECURE_NO_WARNINGS #include<iostream>
using namespace std;
class Person { public:
Person() { cout << "对象构造" << endl; } ~Person() { cout << "对象析构" << endl; } };
double dev(double x, double y) {
Person p1, p2;
if (y == 0) { throw y; }
return x / y; }
void test01() { dev(10,0); }
void test02() { try { test01(); } catch (double y) { cout << "除数为: " << y << endl; } } int main(char *argv[], int argc) { test02(); return 0; }
|
