异常类的构建
异常类
- 列表内容
- 对于异常类型的匹配是至上而下严格匹配的
- 复制兼容性原则依旧适用,因此:
- 匹配子类异常的catch放在上部
- 匹配父类异常的catch放在下部
C++库中必要包含的异常类族:
预备知识点:
1. strdup函数
- 功能: 将串拷贝到新建的位置处
- 用法: char *strdup(char *str);
- 注意:strdup()主要是拷贝字符串s的一个副本,由函数返回值返回,这个副本有自己的内存空间,和s不相干。strdup函数复制一个字符串,使用完后要记得删除在函数中动态申请的内存,strdup函数的参数不能为NULL,一旦为NULL,就会报段错误,因为该函数包括了strlen函数,而该函数参数不能是NULL。
2. 纯虚析构函数的应用
- 在抽象类中,可以给出纯虚函数的定义,但是依旧无法定义对象。(因为纯虚函数无法定义对象(父类),所以纯虚函数无需考虑多态)
- 在C++中一旦标记为纯虚函数,就意味着该类是一个抽象类,因此该类无法定义对象。 有时需要定义一个抽象类,又找不到合适的函数定义为纯虚函数,可以考虑析构函数。
- 析构函数作为纯虚函数的时候要注意: 当析构函数作为纯虚函数的时候依旧需要定义析构函数(不能只声明)。因为派生类的析构函数会自动调用其基类的析构函数。这是一个递归过程,最终抽象类的纯虚析构函数也会被调用。从另外一个角度考虑,抽象类中也可能会申请栈上资源,因此抽象类也需要析构函数进行资源释放。
3. 指针类型的隐式类型转换
- C语言中指针之间可以相互隐式转换,但是会出现警告。因此NULL可以定义成空类型的指针,与0进行区分:
#define NULL ((void*)0)
- C++语言对类型的管控非常严格,无法实现指针间的类型转换。因此C++中给出了新的NULL的定义
#ifdef __cplusplus__
#define NULL 0
#else
#define NULL ((void*)0)//C语言中的NULL
#endlf
但是将NULL直接赋值给0也有缺陷:在函数重载的时候会优先匹配形参为整形(int)的函数,如果想匹配指针类型需要进行强制转换。
3.在C++11提供了nullptr类,运用模板技术可以完美解决该问题。
4. 数组转换成字符串
- itoa函数(windows专用)
用法:char*itoa(int value,char*string,int radix);
int value 被转换的整数,char *string 转换后储存的字符数组,int radix 转换进制数
itoa是广泛应用的非标准C语言和C++语言扩展函数。由于它不是标准C/C++语言函数,所以不能在所有的编译器中使用。但是,大多数的编译器(如Windows上的)通常在 stdlib.h/ cstdlib .h 头文件中包含这个函数 - sprintf函数,哪里均可用。
用法:int sprintf( char *buffer, const char *format, [ argument] … );
参数列表:
buffer:char型指针,指向将要写入的字符串的缓冲区。
format:格式化字符串。
[argument]…:可选参数,可以是任何类型的数据。
5. ANSI C标准中有几个标准预定义宏
__LINE__: 在源码中插入当前源代码行号(一个整数,%d);
__FILE__: 在源文件中插入当前原文件名(一个字符串,%s);
__DATE__: 在源文件中插入当前的编译日期(日:月:年,%s);
__TIME__:当源文件中插入当前编译时间(时:分:秒,%s);
程序
异常的抽象类:
class Exception{
private:
char* message;
char* location;
void Init(const char* mes, const char* file, unsigned int line);
public:
Exception(const char* mes);
Exception(const char* file, int line);
Exception(const char* mes, const char* file, unsigned int line);
Exception( const Exception& e);
Exception& operator = (const Exception& e);
//why use virtual
virtual char* GetMessage() const;
char* GetLocation() const;
virtual ~Exception()= 0;
};这里写代码片
对应的五个异常子类
//Arithmetic Exception
class ArithmeticExcpt: public Exception{
public:
ArithmeticExcpt():Exception(0){};
ArithmeticExcpt(const char* message):Exception(message){};
ArithmeticExcpt(const char* file, int line):Exception(file, line){};
ArithmeticExcpt(const char* message, const char* file, int line):Exception(message, file, line){};
ArithmeticExcpt(const ArithmeticExcpt& ae):Exception(ae){};
ArithmeticExcpt& operator = (const ArithmeticExcpt& ae){ return (*this = ae);};
};
//Index out of bounds exception
class IndexOutOfBoundsExcpt: public Exception{
public:
IndexOutOfBoundsExcpt():Exception(0){};
IndexOutOfBoundsExcpt(const char* message):Exception(message){};
IndexOutOfBoundsExcpt(const char* file, int line):Exception(file, line){};
IndexOutOfBoundsExcpt(const char* message, const char* file, int line):Exception(message, file, line){};
IndexOutOfBoundsExcpt(const IndexOutOfBoundsExcpt& ae):Exception(ae){};
IndexOutOfBoundsExcpt& operator = (const IndexOutOfBoundsExcpt& ae){ return (*this = ae);};
};
//No enough memory exception
class NoEnoughMemoryExcpt: public Exception{
public:
NoEnoughMemoryExcpt():Exception(0){};
NoEnoughMemoryExcpt(const char* message):Exception(message){};
NoEnoughMemoryExcpt(const char* file, int line):Exception(file, line){};
NoEnoughMemoryExcpt(const char* message, const char* file, int line):Exception(message, file, line){};
NoEnoughMemoryExcpt(const NoEnoughMemoryExcpt& ae):Exception(ae){};
NoEnoughMemoryExcpt& operator = (const NoEnoughMemoryExcpt& ae){ return (*this = ae);};
};
//Null point exception
class NullPointExcpt: public Exception{
public:
NullPointExcpt():Exception(0){};
NullPointExcpt(const char* message):Exception(message){};
NullPointExcpt(const char* file, int line):Exception(file, line){};
NullPointExcpt(const char* message, const char* file, int line):Exception(message, file, line){};
NullPointExcpt(const NullPointExcpt& ae):Exception(ae){};
NullPointExcpt& operator = (const NullPointExcpt& ae){ return (*this = ae);};
};
//Invalid Parameter exception
class InvalidParameterExcpt: public Exception{
public:
InvalidParameterExcpt():Exception(0){};
InvalidParameterExcpt(const char* message):Exception(message){};
InvalidParameterExcpt(const char* file, int line):Exception(file, line){};
InvalidParameterExcpt(const char* message, const char* file, int line):Exception(message, file, line){};
InvalidParameterExcpt(const InvalidParameterExcpt& ae):Exception(ae){};
InvalidParameterExcpt& operator = (const InvalidParameterExcpt& ae){ return (*this = ae);};
};