异常和继承

异常也是类,我们可以创建自己的异常类,在异常中可以使用(虚函数,派生,引用传递和数据成员等),

下面用一个自制的数组容器Vector,在对Vector初始化时来对Vector的元素个数进行有效检查。以此来说明继承与异常的使用关系。

运行下方代码,Vector对象传不同参数进去,会触发相应的异常的条件:

1) size < 0 抛出异常 errNegativeException 类

2) size = 0 抛出异常 errZeroException 类

3) size > 1000 抛出异常 errTooBigException  类

4) size < 10 抛出异常 errToosmallException  类

5) errSizeException 类是以上的父类,定义 virtual void printError() const 的虚函数,以多态的形式来输出错误。

  1 #include <iostream>
2
3 using namespace std;
4
5 //errSizeException 父类
6 class errSizeException
7 {
8 public:
9 errSizeException(int size)
10 {
11 m_size = size;
12 }
13 virtual void printError()const //虚函数实现多态
14 {
15 cout << "errSizeException size:" << m_size << endl;
16 }
17
18 protected:
19 int m_size = 0;
20 };
21
22 //index < 0 抛出异常 errNegativeException 类
23 class errNegativeException :public errSizeException
24 {
25 public:
26 errNegativeException(int size) :errSizeException(size) {} //使用初始化列表对子类初始化
27 const virtual void printError()
28 {
29 cout << "errNegativeException size:" << m_size << endl;
30 }
31 };
32
33 //index = 0 抛出异常 errZeroException 类
34 class errZeroException :public errSizeException
35 {
36 public:
37 errZeroException(int size) :errSizeException(size) {} //使用初始化列表对子类初始化
38 const virtual void printError()
39 {
40 cout << "errZeroException size:" << m_size << endl;
41 }
42 };
43
44 //index > 1000 抛出异常 errTooBigException 类
45 class errTooBigException :public errSizeException
46 {
47 public:
48 errTooBigException(int size) :errSizeException(size) {} //使用初始化列表对子类初始化
49 const virtual void printError()
50 {
51 cout << "errTooBigException size:" << m_size << endl;
52 }
53 };
54
55 //index < 10 抛出异常 errToosmallException 类
56 class errToosmallException :public errSizeException
57 {
58 public:
59 errToosmallException(int size) :errSizeException(size) {} //使用初始化列表对子类初始化
60 const virtual void printError()
61 {
62 cout << "errToosmallException size:" << m_size << endl;
63 }
64 };
65
66
67 class Vector
68 {
69 public:
70 Vector(int szie); //带参构造函数
71 ~Vector() //析构函数
72 {
73 if (m_base)
74 {
75 delete[] m_base;
76 m_len = 0;
77 }
78 }
79
80 int getLength() //获取内部储存元素的个数
81 {
82 return m_len;
83 }
84
85 int& operator[](int index) //下标重载
86 {
87 return m_base[index];
88 }
89 private:
90 int* m_base = NULL; //存储空间指针
91 int m_len = 0; //长度
92 };
93
94 Vector::Vector(int len)
95 {
96 if (len < 0)
97 {
98 throw errNegativeException(len); //抛出临时对象,带有长度参数进行构造
99 }
100 else if (len = 0)
101 {
102 throw errZeroException(len); //抛出临时对象,带有长度参数进行构造
103 }
104 else if (len > 1000)
105 {
106 throw errTooBigException(len); //抛出临时对象,带有长度参数进行构造
107 }
108 else if (len < 10)
109 {
110 throw errToosmallException(len); //抛出临时对象,带有长度参数进行构造
111 }
112 m_len = len;
113 m_base = new int[m_len];
114 }
115
116
117 int main()
118 {
119 try
120 {
121 Vector _V(-1); //传:-1、0、1001、1 四个参数进去,会抛出四个异常
122
123 for (int i = 0; i < _V.getLength(); i++)
124 {
125 _V[i] = i + 10;
126 printf("_V[%d]:%d\n", i, _V[i]);
127 }
128 }
129 catch (const errSizeException& err) //父类,由多态特性与其子类进行匹配
130 {
131 err.printError();
132 }
133
134 return 0;
135 }

1. 异常处理的三个关键字

点击查看

2. 异常处理的基本语法

点击查看

3.异常处理接口声明

点击查看

4.异常类型的生命周期

4.1 throw 基本类型:

点击查看

4.2 throw 字符串类型:

点击查看

4.3 throw 类类型异常:

点击查看

5.异常和继承

点击查看

6.异常处理的基本思想

点击查看

7.标准库里的异常类

点击查看

最新文章

  1. Sql Server系列:DBCC命令
  2. 更新日志 - BugHD 新增邮件告警功能
  3. CSS3的chapter3
  4. java学习第九天
  5. [HIHO1184]连通性二&#183;边的双连通分量(双连通分量)
  6. Android常见开发思路
  7. MySQL注入中load_file()函数的应用
  8. javascript 与 java
  9. 转:Eclipse Kepler已支持Java 8
  10. Android 三星手机不能调起应用市场
  11. PKU A Simple Problem with Integers (段树更新间隔总和)
  12. 谁说深入浅出虚拟机难?现在我让他通俗易懂(JVM)
  13. springboot~JPA把ORM统一起来
  14. 渗透之Empire
  15. Spring Boot笔记二:快速创建以及yml文件自动注入
  16. Linux安装NET CORE
  17. passive 的事件监听器(转载)
  18. 原来CNN是这样提取图像特征的。。。
  19. linux测试工程介绍(Linux Test Project)
  20. ACM题目————STL + 全排列

热门文章

  1. Python网络编程_抓取百度首页代码(注释详细)
  2. SpringBoot 之 @ControllerAdvice 拦截异常并统一处理
  3. Hibernate初识
  4. bWAPP----Mail Header Injection (SMTP)
  5. 标准库之time,random,sys,os
  6. js 手机号验证
  7. Luogu P2656 采蘑菇
  8. Java基础教程——序列化
  9. Java中的单例模式最全解析
  10. Java集合【7】--List接口超级详细解析