add Chapter01 examples
This commit is contained in:
31
Chapter01/exceptions.cpp
Normal file
31
Chapter01/exceptions.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <cstdio>
|
||||
|
||||
struct A {
|
||||
A() { printf("A is created!\r\n"); }
|
||||
~A() { printf("A is destroyed!\r\n"); }
|
||||
};
|
||||
|
||||
struct B {
|
||||
B() { printf("B is created!\r\n"); }
|
||||
~B() { printf("B is destroyed!\r\n"); }
|
||||
};
|
||||
|
||||
void bar() {
|
||||
B b;
|
||||
throw 0;
|
||||
}
|
||||
|
||||
void foo() {
|
||||
A a;
|
||||
bar();
|
||||
A a1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
try {
|
||||
foo();
|
||||
} catch (int &p) {
|
||||
printf("Catching an exception!\r\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user