异常类之父类

上一节已经实现了异常基类的功能,接下来将实现基于该基类的几个父类

功能定义如下


异常类


功能描述


ArithmeticException


计算异常


NullPointerException


空指针异常


IndexOutOfBoundsException


越界异常


NotEnoughMemoryException


内存不足异常


InvalidParameterException


参数错误异常

为了代码简洁,定义一个宏

#define THROW_EXCEPTION(e, m) (throw e(m, __FILE__, __LINE__))//e为异常类类型

如果这个宏看不懂,就想一下throw函数的使用(可参见上一节)

要做的很简单,就是继承这个基类

完整代码如下

Exception.h

  1 #ifndef _EXCEPTION_H_
  2 #define _EXCEPTION_H_
  3
  4
  5
  6 namespace DataStructureLib
  7 {
  8 #define THROW_EXCEPTION(e,m) throw e(m,__FILE__,__LINE__)
  9 class Exception
 10 {
 11 protected:
 12     char*  m_message;
 13     char*  m_location;
 14 protected:
 15     void  init(const char*,const char*,int);//由于三个构造函数中的逻辑很相似,所以可以将相似的部分统一放到一个函数init()
 16 public:
 17     Exception(const char* message);
 18     Exception(const char* file,int line);
 19     Exception(const char* message,const char* file,int line);
 20
 21     //涉及到堆空间即需进行深拷贝,拷贝构造函数和"="
 22     Exception(const Exception& e);
 23     Exception& operator =(const Exception& e);
 24
 25     virtual const char* message() const;
 26     virtual const char* location() const;
 27
 28     virtual ~Exception(void)= 0;
 29 };
 30 //计算异常类
 31 class ArithmeticException: public Exception
 32 {
 33 public:
 34     ArithmeticException():Exception(0){}
 35     ArithmeticException(const char* message):Exception(message){}
 36     ArithmeticException(const char*file, int line):Exception(file, line){}
 37     ArithmeticException(const char *message, const char* file, int line):Exception(message, file, line){}
 38
 39     ArithmeticException(const ArithmeticException& e): Exception(e){}
 40     ArithmeticException& operator=(const ArithmeticException& e)
 41     {
 42         Exception::operator =(e);
 43
 44         return *this;
 45     }
 46 };
 47
 48
 49
 50
 51 //空指针异常
 52 class NullPointerException: public Exception
 53 {
 54 public:
 55     NullPointerException():Exception(0){}
 56     NullPointerException(const char* message):Exception(message){}
 57     NullPointerException(const char*file, int line):Exception(file, line){}
 58     NullPointerException(const char *message, const char* file, int line):Exception(message, file, line){}
 59
 60     NullPointerException(const NullPointerException& e): Exception(e){}
 61     NullPointerException& operator=(const NullPointerException& e)
 62     {
 63         Exception::operator =(e);
 64
 65         return *this;
 66     }
 67 };
 68
 69 //越界异常类
 70 class IndexOutOfBoundsException: public Exception
 71 {
 72 public:
 73     IndexOutOfBoundsException():Exception(0){}
 74     IndexOutOfBoundsException(const char* message):Exception(message){}
 75     IndexOutOfBoundsException(const char*file, int line):Exception(file, line){}
 76     IndexOutOfBoundsException(const char *message, const char* file, int line):Exception(message, file, line){}
 77
 78     IndexOutOfBoundsException(const IndexOutOfBoundsException& e): Exception(e){}
 79     IndexOutOfBoundsException& operator=(const IndexOutOfBoundsException& e)
 80     {
 81         Exception::operator =(e);
 82
 83         return *this;
 84     }
 85 };
 86
 87 //内存不足异常类
 88 class NotEnoughMemoryException: public Exception
 89 {
 90 public:
 91     NotEnoughMemoryException():Exception(0){}
 92     NotEnoughMemoryException(const char* message):Exception(message){}
 93     NotEnoughMemoryException(const char*file, int line):Exception(file, line){}
 94     NotEnoughMemoryException(const char *message, const char* file, int line):Exception(message, file, line){}
 95
 96     NotEnoughMemoryException(const NotEnoughMemoryException& e): Exception(e){}
 97     NotEnoughMemoryException& operator=(const NotEnoughMemoryException& e)
 98     {
 99         Exception::operator =(e);
100
101         return *this;
102     }
103 };
104
105 //参数错误异常类
106 class InvalidParameterException: public Exception
107 {
108 public:
109     InvalidParameterException():Exception(0){}
110     InvalidParameterException(const char* message):Exception(message){}
111     InvalidParameterException(const char*file, int line):Exception(file, line){}
112     InvalidParameterException(const char *message, const char* file, int line):Exception(message, file, line){}
113
114     InvalidParameterException(const InvalidParameterException& e): Exception(e){}
115     InvalidParameterException& operator=(const InvalidParameterException& e)
116     {
117         Exception::operator =(e);
118
119         return *this;
120     }
121 };
122
123 #endif
124 }

Exception.cpp

 1 #include "Exception.h"
 2 #include <cstring>
 3 #include <cstdlib>
 4
 5 namespace DataStructureLib
 6 {
 7 void Exception::init(const char* message,const char* file,int line)
 8 {
 9     m_message=strdup(message);//这里不能直接使用m_message=message,
10                             //因为message指针指向的数据有可能会在栈、堆、全局区,如果是在栈上,局部变量可能会消失,如果直接使用m_message=message就会不安全
11     if(file!=NULL)
12     {
13         char sl[16]={0};
14         itoa(line,sl,10);//将line转为char类型 10代表十进制
15
16         m_location=static_cast<char* >(malloc(strlen(file)+strlen(sl)+2));//加2表示后面的":"和结束符即“/0”
17         m_location=strcpy(m_location,file);
18         m_location=strcat(m_location,":");
19         m_location=strcat(m_location,sl);
20     }
21 }
22
23 Exception::    Exception(const char* message)
24 {
25     init(message,NULL,0);
26 }
27
28 Exception::Exception(const char* file,int line)
29 {
30     init(NULL,file,line);
31 }
32
33 Exception::Exception(const char* message,const char* file,int line)
34 {
35     init(message,file,line);
36 }
37
38 Exception::~Exception(void)
39 {
40
41     free(m_message);
42     free(m_location);
43 }
44
45 const char* Exception::message() const
46 {
47     return m_message;
48 }
49
50 const char* Exception::location() const
51 {
52     return m_location;
53 }
54
55 Exception::Exception(const Exception& e)
56 {
57     m_message=strdup(e.m_message);
58     m_location=strdup(e.m_location);
59 }
60
61 Exception& Exception::operator =(const Exception& e)
62 {
63     if (this!=&e)
64     {
65         free(m_message);
66         free(m_location);
67
68         m_message=strdup(e.m_message);
69         m_location=strdup(e.m_location);
70     }
71     return *this;
72 }
73
74 }

测试

 1 #include <iostream>
 2 #include "Exception.h"
 3
 4 using namespace std;
 5 using namespace DataStructureLib;
 6
 7 int main()
 8 {
 9     try{
10         THROW_EXCEPTION(NullPointerException,"test");
11     }
12
13     catch(const ArithmeticException& e)
14     {
15         cout<<"ArithmeticException "<<endl;
16         cout<<e.location()<<endl;
17         cout<<e.message()<<endl;
18     }
19     catch(const NullPointerException& e)
20     {
21         cout<<"NullPointerException"<<endl;
22         cout<<e.location()<<endl;
23         cout<<e.message()<<endl;
24     }
25
26     catch(const Exception& e)
27     {
28         cout<<"Exception"<<endl;
29         cout<<e.location()<<endl;
30         cout<<e.message()<<endl;
31     }
32
33     system("pause");
34     return 0;
35 }

原文地址:https://www.cnblogs.com/zhaobinyouth/p/9557782.html

时间: 2024-07-29 11:13:37

异常类之父类的相关文章

类的继承与super()的意义以即如何写一个正确的异常类

这些东西都是我看了许多名师课程和自己研究的成果,严禁转载,这里指出了如何正确的自己定义一个异常类并看一看sun写的java的源代码话题一:子类的构造器执行是否一定会伴随着父类的构造执行? 1.this()与super()不能共存2.如果不显示地调用this()和super();子类对象的创建是否一定执行父类的构造3.既然super()和this()不能共存,又说子类的构造执行一定会执行父类的构造,那么我让子类的构造执行this()是不是就不能在执行父类的构造? 4.如果我非不让父类的构造执行,我

java中常见的异常类

1. java.lang.nullpointerexception   这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用了未经初始化的对象或者是不存在的对象,这个错误经常出现在创建图片,调用数组这些操作中,比如图片未经初始化,或者图片创建时的路径错误等等.对数组操作中出现空指针,很多情况下是一些刚开始学习编程的朋友常犯的错误,即把数组的初始化和数组元素的初始化混淆起来了.数组的初始化是对数组分配需要的空间,而初始化后的数组,其中的元素并没有实例化,依然是空

通过寄生组合式继承创建js的异常类

最近项目中在做js的统一的异常处理,需要自定义异常类.理想的设计方案为:自定义一个异常错误类BaseError,继承自Error,然后再自定义若干个系统异常,例如用户取消异常.表单异常.网络异常,继承自BaseError.系统中,根据各个自定义异常做统一的异常处理,例如如果是用户发出取消操作指令,当前调用链则抛出一个用户取消异常,然后由统一异常处理捕获,先判断他是不是继承自BaseError,如果是则再根据事先定义好的处理方案处理. 为啥说这只是理想的设计方案呢?因为es5根本就没有提供js的继

如何自定义一个异常类

如何自己定义一个异常类来管理相应的异常? 1) 为这个找一个近似的类作为父类. 2) 在该类中编写两个构造器: a) 默认构造器; b) 带String message参数的构造器且在该构造器中使用 super(message); 语句     来调用父类的构造器完成异常原因的更改. 以下实例为,创建一个小猫对象,其年龄为负数则显示为异常 public class Exception7 { public static void main(String[] args) { Cat c1=null;

跟王老师学异常(三)异常类的继承体系

异常类的继承体系 主讲人:王少华  QQ群号:483773664 学习目标: 1.掌握异常的体系 2.掌握处理异常类的几中常用方法 一.异常类继承体系图 Java提供了丰富的异常类,这些异常类之间有严格的继承关系,如下图所示 从上图可以看出,Java把所有非正常情况分成两种,一种是异常(Exception),另一种是错误(Error),它们都继承Throwable父类. 二.Error Error错误,一般是指虚拟机相关的问题.即仅靠程序本身无法恢复的严重错误.如系统崩溃.虚拟机出错误.动态链接

08. Object类、异常类

八.黑马程序员_Object类.异常类 A.Object类  B.异常类 A.Object类介绍 a.介绍 Object类是所有类的直接或间接父类,它里面定义的功能是所有类都具备的. b.Object类中常用的方法有 getClass():返回该对像的运行时类.它返回一个Class类型的对像.在反射中较常用: toString():返回该对像的字符串表现形式.返回值为String类型.通常我们都要覆写这个方法: hashCode():返回该对象的哈希码值.它返回一个int类型的值.通常情况下我们

第十一课、异常类的构建-------------狄泰软件学院

一.自定义异常类 1.异常的类型可以是自定义的类类型 2.对于类类型的匹配依旧是之上而下的严格匹配 3.赋值兼容性原则在异常匹配中依然适用 所以要 (1).匹配子类异常的catch放在上部 (2).匹配父类异常的catch放在下部 4.异常类是数据结构所依赖的"基础设施"(现代c++库也必然包含充要的异常类族) 二.一步步打造自己的异常类 1.首先是抽象类EXception的编写,既然是抽象类,必然含有纯虚函数,通常的做法都是将析构函数作为纯虚函数 头文件:接口定义 class Exc

Java学习(异常类)

一.什么是异常: 异常就是在运行时产生的问题.通常用Exception描述. 在java中,把异常封装成了一个类,当出现问题时,就会创建异常类对象并抛出异常相关的信息(如详细信息,名称以及异常所处的位置). 二.异常的继承关系: Throwable类是所有错误跟异常类的超类(祖宗类). Exception异常类及其子类都是继承自Throwable类,用来表示java中可能出现的异常,并且合理的处理这些异常. RuntimeException类是运行异常类,继承自Exception类,它以及它的子

异常类的构建(四)

我们在之前学习了 C++ 中有关异常的知识,现在我们来重新回顾下.那么异常的格式是什么呢?便是 try ... catch ...:try 语句处理正常的代码逻辑,而 catch 语句则处理异常情况,try 语句中的异常由对应的 catch 语句处理.格式如下 try {     double r = divide(1, 0); } catch(...) {     cout << "Divided by zero ..." << endl; } 在 C++ 中