-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexceptionStudy.h
More file actions
72 lines (65 loc) · 977 Bytes
/
exceptionStudy.h
File metadata and controls
72 lines (65 loc) · 977 Bytes
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#include <iostream>
#include <stdexcept>
using namespace std;
void testTryException()
{
int input = -1;
try
{
cout << "enter -1:" << endl;
cin >> input;
cin.get();
int *num = new int[input];
delete num;
}
catch (...)
{
cout << "exception is catched" << endl;
}
}
class X
{
public :
class Trouble {};
class Small : public Trouble {};
class Big :public Trouble {};
void f() { throw Big(); }
};
void testTryThrowException()
{
X x;
try
{
x.f();
}
/*
捕获总是会捕获到基类中,如果像使用到派生类,那么应该将派生类的异常捕获放到最前面
*/
catch (X::Small&)
{
cout << "Small" << endl;
}
catch (X::Big&)
{
cout << "Big" << endl;
}
catch (X::Trouble&)
{
cout << "Trouble" << endl;
}
}
class MyError :public runtime_error {
public:
MyError(const string & msg = "") :runtime_error(msg) {}
};
void testMyErrorException()
{
try {
throw MyError("my message exception");
}
catch (MyError &x)
{
cout<<x.what();
}
}