相关内容:
- 0、异常的类型可以是自定义类类型,对于类类型异常的匹配依旧是至上而下严格匹配,赋值兼容性原则在异常匹配中依然适用。一般而言,将匹配子类异常的
catch
放在上部,匹配父类异常的catch
放在下部。在工程中会定义一系列的异常类,每个类代表工程中可能出现的一种异常类型。 - 1、
C++
中直接支持异常处理的概念; - 2、
try...catch..
是C++
中异常处理的专用语句; - 3、
try
语句处理正常代码逻辑,catch
语句处理异常情况,同一个try
语句可以跟上多个catch
语句; - 4、异常处理必须严格匹配,不进行任何的转换;
- 5、
catch
语句块中可以抛出异常,异常的类型可以是自定义类类型; - 6、赋值兼容性原则在异常匹配中依然适用;
- 7、标准库中的异常都是从
exception
类派生的。代码相关内容:
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using namespace std;
const int DefaultSize = 10;
//动态数组
class Array
{
public:
Array(int itsSize = DefaultSize);
~Array() { delete[] pType;} // 一般而言,在构造函数中new,则在析构函数中都要delete,new和delete一般配对使用,如果忘记delete,可能出现内存泄漏问题;
// 运算符重载
int& operator[](int offSet);
const int& operator[](int offSet) const;
//访问器,accessors
int GetitsSize() const { return itsSize; }
//异常类
class xBoundary{}; //用于检测小标越界引发的异常;
class xSize //写为类之后可以进行集成,然后具体的异常类都会简化;
{
public:
xSize() {};
xSize(int size):itsSize(size) {}
~xSize() {}
int GetSize() { return itsSize; }
virtual void PrintError()
{
cout << "下标发生错误:" << itsSize << endl;
}
protected:
int itsSize;
};
class xZero : public xSize
{
public:
xZero(int size) : xSize(size) {};
virtual void PrintError()
{
cout << "下标不能是0!" << endl;
}
};
class xNegative : public xSize
{
public:
xNegative(int size) : xSize(size) {};
virtual void PrintError()
{
cout << "下标不能是负的!" << xSize::itsSize << endl;
}
};
class xTooSmall : public xSize
{
public:
xTooSmall(int size) : xSize(size) {};
virtual void PrintError()
{
cout << "下标不能小于10:" << xSize::itsSize << endl;
}
};
class xTooBig : public xSize
{
public:
xTooBig(int size) : xSize(size) {};
virtual void PrintError()
{
cout << "下标不能大于30000:" << xSize::itsSize << endl;
}
};
private:
int *pType;
int itsSize;
};
//const用来读,非const用来写;
//非const函数的运算符重载,如果下标超过应有的大小,不在范围之内,就跑出异常;
int& Array::operator[](int offSet)
{
int size = this->GetitsSize();
if (offSet >= 0 && offSet < size)
{
return pType[offSet];
}
throw xBoundary();
}
//const函数的运算符重载,如果下标超过应有的大小,不在范围之内,就跑出异常;
const int& Array::operator[](int offSet) const
{
int size = this->GetitsSize();
if (offSet >= 0 && offSet < size)
{
return pType[offSet];
}
throw xBoundary();
}
Array::Array(int size) : itsSize(size)
{
if (size == 0)
throw xZero(size);
else if (size < 0)
throw xNegative(size);
else if (size < 10)
throw xTooSmall(size);
else if (size > 30000) //此处的10和30000都只是进行说明,c++中并没有要求必须小于30000大于10;
throw xTooBig(size);
pType = new int[size];
for (int i = 0; i < size; i++)
pType[i] = 0;
}
int main()
{
try //将可能发生异常的代码都放在try里面
{
Array a;
Array intArray(20);
Array b(6);
/*b[6] = 88;
b[22] = 9;
cout << b[6] << endl;
cout << "测试一下" << endl;*/
//没有越界的都进行正常输出,越界的将检测到xBoundary然后跑出异常;
for (int j = 0; j < 100; j++)
{
intArray[j] = j;
cout << "intArray[" << j << "] okay..." << endl;
}
}
catch (Array::xBoundary)
{
cout << "下标越界了。" << endl;
}
catch (Array::xSize& exp) //通过此手段就可以代替下面的catch,当然,此处简单了,在设计具体的异常类比如xZero的时候就需要多些一些东西,针对此处具体来说就是要对继承的虚函数重新进行定义;
{
exp.PrintError();
}
//catch (Array::xZero theException)
//{
// cout << "下标不能是0." << endl;
//}
//catch (Array::xNegative theException) //通过变量theException来检查size;
//{
// cout << "下标不能是负数:" << theException.GetSize() << endl;
//}
//catch (Array::xTooSmall theException)
//{
// cout << "下标不能小于10:" << theException.GetSize() << endl;
//}
//catch (Array::xTooBig theException)
//{
// cout << "下标不能大于30000:" << theException.GetSize() << endl;
//}
catch (...)
{
cout << "" << endl;
}
return 0;
}代码执行结果:
参考链接:
vs静态编译解决找不到msvcp140.dll问题
C++ 中的异常处理(五十三)