【C++异常】

目录

1.异常的概念

2.异常的使用

3.异常导致内存泄漏 

3.1异常的重新抛出 


C语言的传统处理错误方式

  1. 错误码:使用返回的错误码,自己去查找对应的错误。
  2. assert断言(头文件assert.h):比较常用但是发生错误会终止程序; 

1.异常的概念

  • 使用异常处理错误不会结束掉程序

异常是一种处理错误的方式,当一个函数发现自己无法处理的错误时就可以抛出异常,让函数的直接或间接的调用者处理这个错误(捕获的位置不同)

格式:

void func()
{
	if (条件)
		throw 抛出类型;
	else
		正常执行
}

int main()
{
	try
	{
		func();//函数
	}
	catch (接受类型)
	{

	}
	catch (接受类型)
	{

	}
	catch (...)//接受任意类型,防止抛出的异常没有接受导致程序崩溃
	{

	}

}

例子:一个除法函数,除数不能为0; 

double Division(int a, int b)
{
 // 当b == 0时抛出异常
 if (b == 0)
 throw "Division by zero condition!";
 else
 return ((double)a / (double)b);
}
void Func()
{
 int len, time;
 cin >> len >> time;
 cout << Division(len, time) << endl;
}
int main()
{
	try {
		Func();
	}
	catch (const char* errmsg){
		cout << errmsg << endl;
	}
	catch (...) {
		cout << "unkown exception" << endl;
	}
		return 0;
}

输入:9 3 

输入:9 0

2.异常的使用

2.1.异常的抛出和匹配原则 

  1. 异常是通过抛出对象而引发的,该对象的类型决定了应该激活哪个catch的处理代码(找匹配的)。
  2. 被选中的处理代码是调用链中与该对象类型匹配且离抛出异常位置最近的那一个。
    double Division(int a, int b)
    {
    	// 当b == 0时抛出异常
    	if (b == 0)
    		throw "除0错误";
    	else
    		return ((double)a / (double)b);
    }
    void Func()
    {
    	try
    	{
    		int len, time;
    		cin >> len >> time;
    		cout << Division(len, time) << endl;
    	}
    	catch (const char* errmsg) {//被这个捕获,匹配且最近
    		cout << errmsg << endl;
    	}
    }
    int main()
    {
    	try {
    		Func();
    	}
    	catch (const char* errmsg) {//匹配
    		cout << errmsg << endl;
    	}
    	catch (...) {//匹配
    		cout << "unkown exception" << endl;
    	}
    	return 0;
    }
  3. 抛出异常对象后,会生成一个异常对象的拷贝,因为抛出的异常对象可能是一个临时对象, 所以会生成一个拷贝对象,这个拷贝的临时对象会在被catch以后销毁。(这里的处理类似于函数的传值返回)
  4. catch(...)可以捕获任意类型的异常,问题是不知道异常错误是什么(必须是最后一个捕获,它匹配所以类型)
  5. 实际中抛出和捕获的匹配原则有个例外,并不都是类型完全匹配,可以抛出的派生类对象, 使用基类捕获(切片行为)

栈展开:

  1. 首先检查throw本身是否在try块内部,如果是再查找匹配的catch语句。如果有匹配的,则调到catch的地方进行处理
  2. 没有匹配的catch则退出当前函数栈,继续在调用函数的栈中进行查找匹配的catch
  3. 如果到达main函数的栈,依旧没有匹配的,则终止程序。上述这个沿着调用链查找匹配的catch子句的过程称为栈展开。所以实际中我们最后都要加一个catch(...)捕获任意类型的异 常,否则当有异常没捕获,程序就会直接终止。
  4. 如果不捕获程序会终止,下面那个错误,所以说捕获列表最后加一个catch(...)很有必要(不是必加,看需求)
double Division(int a, int b)
{
	// 当b == 0时抛出异常
	if (b == 0)
		throw "除0错误";
	else
		return ((double)a / (double)b);
}
void Func()
{
	try
	{
		int len, time;
		cin >> len >> time;
		cout << Division(len, time) << endl;
	}
	catch (const int* errmsg) {
		cout << errmsg << endl;
	}
}
int main()
{
	try {
		Func();
	}
	catch (const string* errmsg) {
		cout << errmsg << endl;
	}
	catch (...) {
		cout << "unkown exception" << endl;
	}
	return 0;
}

 

3.异常导致内存泄漏 

如果抛异常了,每次按调用链找catch子句首先看throw是否在try块中,再看是否有匹配的

下面代码直接去main找catch子句不会调delete

double Division(int a, int b)
{
	// 当b == 0时抛出异常
	if (b == 0)
	{
		throw "Division by zero condition!";
	}
	return (double)a / (double)b;
}
void Func()
{
	int* array = new int[10];
		int len, time;
		cin >> len >> time;
		cout << Division(len, time) << endl;
	cout << "delete []" << array << endl;
	delete[] array;
}
int main()
{
	try
	{
		Func();
	}
	catch (const char* errmsg)
	{
		cout << errmsg << endl;
	}
	return 0;
}

3.1异常的重新抛出 

解决方法1:使用catch(...)捕获,调用delete再抛出异常

double Division(int a, int b)
{
	// 当b == 0时抛出异常
	if (b == 0)
	{
		throw "Division by zero condition!";
	}
	return (double)a / (double)b;
}
void Func()
{
	// 这里可以看到如果发生除0错误抛出异常,另外下面的array没有得到释放。
	// 所以这里捕获异常后并不处理异常,异常还是交给外面处理,这里捕获了再
	// 重新抛出去。
	int* array = new int[10];
	try {
		int len, time;
		cin >> len >> time;
		cout << Division(len, time) << endl;
	}
	catch (...)
	{
		cout << "delete []" << array << endl;
		delete[] array;
		throw;
	}
	cout << "delete []" << array << endl;
	delete[] array;
}
int main()
{
	try
	{
		Func();
	}
	catch (const char* errmsg)
	{
		cout << errmsg << endl;
	}
	return 0;
}

 3.2智能指针,看下一篇博客

double Division(int a, int b)
{
	// 当b == 0时抛出异常
	if (b == 0)
	{
		throw "Division by zero condition!";
	}
	return (double)a / (double)b;
}
void Func()
{
	int* array = new int[10];
	unique_ptr<int> u(array);
	int len, time;
	cin >> len >> time;
	cout << Division(len, time) << endl;
}
int main()
{
	try
	{
		Func();
	}
	catch (const char* errmsg)
	{
		cout << errmsg << endl;
	}
	return 0;
}

3.2 异常规范 

  • 格式:函数名后 throw(类型,类型,类型);
  • 表示会抛出什么类型的异常,throw()表示不会抛异常

void func() throw()//不会抛异常
{

}
void func() throw(int, string);//抛出其中一个类型的异常

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值