首页 > 编程语言 > 详细

C++异常之五 异常和继承

时间:2020-03-20 09:11:59      阅读:53      评论:0      收藏:0      [点我收藏+]

异常和继承

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

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

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

1) index < 0 抛出异常 errNegativeException 类

2) index = 0 抛出异常 errZeroException 类

3) index > 1000 抛出异常 errTooBigException  类

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

 

 

 

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

点击查看

2. 异常处理的基本语法

点击查看

3.异常处理接口声明

点击查看

4.异常类型的生命周期

4.1 throw 基本类型:

点击查看

4.2 throw 字符串类型:

点击查看

4.3 throw 类类型异常:

点击查看

5.异常和继承

点击查看

6.异常处理的基本思想

点击查看 

7.标准库里的异常类

点击查看

C++异常之五 异常和继承

原文:https://www.cnblogs.com/CooCoChoco/p/12528858.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!