-
Notifications
You must be signed in to change notification settings - Fork 1
/
异常处理.cpp
52 lines (46 loc) · 961 Bytes
/
异常处理.cpp
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
50
51
52
/*
* @Author: your name
* @Date: 2020-03-11 18:07:17
* @LastEditTime: 2020-04-01 11:01:51
* @LastEditors: Please set LastEditors
* @Description: Exception
* @FilePath: /exception/1.cpp
*/
// 在运行期间转移程序控制权的方式
#include <iostream>
#include <exception>
#include <string>
using namespace std;
class MyException : public exception {
public:
string msg() const{ // 此项不是继承的
return "my exception\n";
}
};
class A {
public:
A(){cout << "A construct\n "; }
~A(){cout << "A distruct\n"; }
};
int main() {
try{
A a;
// 在抛出异常时会自动调用析构函数;
throw MyException();
} catch (MyException &e) {
std::cout << "MyException\n";
std::cout << e.msg();
}
return 0;
}
/*
int main() {
string s("12");
char b;
try {
b = s.at(3);
}catch (exception e) {
cout << e.what() << endl;
}
}
*/