mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-02-03 18:43:52 +08:00
support bazel complie this project and format code.
This commit is contained in:
23
learn_class/modern_cpp_30/RAII/stack.cpp
Normal file
23
learn_class/modern_cpp_30/RAII/stack.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user