构建异常类族

异常类知识点:

  • 异常类的类型可以是自定义类型
  • 对于类类型异常的匹配依旧是从上而下严格匹配
  • 赋值兼容性原则在异常匹配中依然适用
  • 一般而言,异常匹配中,子类异常放于catch上部,父类在下部
  • 创建异常子类是为了可以抛出不同的异常来提示人们,是什么原因导致的

异常类族成员及功能:

异常类功能定义
Exception顶层抽象父类
ArithmeticException计算异常
NullPointerException空指针异常
IndexOutOfBoundsException越界异常
NoEnoughMemoryException内存不足异常
InvalidParameterException参数错误异常

异常类设计原则:
  - 在可复用代码库设计时尽量使用面向对象技术进行架构
  - 尽量使用异常处理机制分离正常逻辑与异常逻辑

异常类中的接口定义:

//利用宏快速的扔出异常
#define THROW_EXCEPTION(e,m) throw(e(m,__FILE__,__LINE__))//扔出一个e类型对象的异常

class Exception:public Object	//顶层抽象父类
{
protected:
	char* m_message;//异常信息
	char* m_location;//异常位置

	void init(const char* message,const char* file,int line);
public:
    Exception(const char* message);
	Exception(const char* file,int line);
	Exception(const char* message,const char* file,int line);

	Exception(const Exception& e);
	Exception& operator =(const Exception& e);

	virtual const char* message()const;
	virtual const char* location()const;

	virtual ~Exception()=0;//纯虚函数,抽象类,只能继承,不能创建对象
};

异常类的具体实现:

#include "Exception.h"

#include <cstdlib>
#include <cstring>

using namespace std;

namespace JYlib
{

void Exception::init(const char* message,const char* file,int line)
{
	m_message = (message != NULL ? strdup(message) : NULL);//申请内存空间,拷贝字符串,返回地址,需要free

	if( file != NULL)
	{
		char str1[16]={0};

		itoa(line,str1,10);//待转换的整数值,存储的字符串,转换为几进制数的字符串

		m_location = reinterpret_cast<char*>(malloc(strlen(file) + strlen(str1) + 2));//多申请的两个字节,存放:与末尾的/0
		if(m_location != NULL)
		{
			m_location = strcpy(m_location,file);//将file拷贝(覆盖)到m_location
			m_location = strcat(m_location,":");//拼接,:接在m_location后面
			m_location = strcat(m_location,str1);
		}
		//这里不能抛出内存不足异常,因为内存不足异常是该类的子类,创建的时候会重新进人该函数,所以直接不处理
	}
	else
	{
		m_location = NULL;
	}
}

Exception::Exception(const char* message,const char* file,int line)
{
	init(message,file,line);
}

Exception::Exception(const char* file,int line)
{
	init(NULL,file,line);
}

Exception::Exception(const char* message)
{
	init(message,NULL,0);
}

Exception::Exception(const Exception& e)//深拷贝
{
	m_message = strdup(e.m_message);
	m_location = strdup(e.m_location);
}

Exception& Exception::operator = (const Exception& e)//深拷贝
{
	if(this != &e)//拷贝构造一定要判断是否为本身
	{
		free(m_message);//先释放原来的内容
		free(m_location);
		m_message = strdup(e.m_message);
		m_location = strdup(e.m_location);
	}
	return *this;
}

const char* Exception::message()const
{
	return m_message;
}

const char* Exception::location()const
{
	return m_location;
}

Exception::~Exception()
{
	free(m_message);
	free(m_location);
}

}

异常子类的实现:这里举一个例子,其余的异常子类实现方式相同

class ArithmeticException : public Exception  //计算异常
{
public:
	ArithmeticException() : Exception(NULL,NULL,0){}
	ArithmeticException(const char* message) : Exception(message,NULL,0){}
	ArithmeticException(const char* file,int line) : Exception(NULL,file,line){}
	ArithmeticException(const char* message,const char* file,int line) : Exception(message,file,line){}
	ArithmeticException(const ArithmeticException& e) : Exception(e){}
	ArithmeticException& operator =(const ArithmeticException& e)
	{
		Exception::operator=(e);

		return *this;
	}
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值