This commit is contained in:
Light-City
2019-12-09 11:14:25 +08:00
parent f98a4ce2c2
commit 3ae20ae621
6 changed files with 49 additions and 3 deletions

View File

@@ -0,0 +1,26 @@
//
// Created by light on 19-12-9.
//
#include <iostream>
class Obj {
public:
Obj() { puts("Obj()"); }
~Obj() { puts("~Obj()"); }
};
void foo(int n)
{
Obj obj;
if (n == 42)
throw "life, the universe and everything";
}
// 不管是否发生了异常obj 的析构函数都会得到执行。
int main()
{
try {
foo(41);
foo(42);
}
catch (const char* s) {
puts(s);
}
}