zl程序教程

您现在的位置是:首页 >  其他

当前栏目

C++复习笔记22

2023-04-18 16:46:51 时间

异常概念:

1. 终止程序,如 assert ,缺陷:用户难以接受。如发生内存错误,除 0 错误时就会终止程序。
2. 返回错误码 ,缺陷:需要程序员自己去查找对应的错误。如系统的很多库的接口函数都是通过把错误码放到 errno 中,表示错误
3. C 标准库中 setjmp longjmp 组合。这个不是很常用,了解一下
实际中 C 语言基本都是使用返回错误码的方式处理错误,部分情况下使用终止程序处理非常严重的错误。
       异常是一种处理错误的方式, 当一个函数发现自己无法处理的错误时就可以抛出异常,让函数的直接或间接的调用者处理这个错误
        throw: 当问题出现时,程序会抛出一个异常。这是通过使用 throw 关键字来完成的。
        catch: 在您想要处理问题的地方,通过异常处理程序捕获异常 . catch 关键字用于捕获异常,可以有多个 catch 进行捕获。
         try: try 块中的代码标识将被激活的特定异常 , 它后面通常跟着一个或多个 catch 块。如果有一个块抛出一个异常,捕获异常的方法会使用 try catch 关键字。 try 块中放置可能抛出异常的代码, try 块中的代码被称为保护代码。使用 try/catch 语句的语法如下所示:
#include<iostream>
#include<assert.h>
//异常
using namespace std;

int Div(int a, int b)
{
	/*if (b == 0)
	{
		return 0;
	}*/
	//assert(b != 0);//assert只能在debug版本下使用
	double c = 1.0;
	string s = "";
	if (b == 0)
		throw(s);//抛出类型而非抛出值
	return a / b;
}

void test01()
{
	try
	{
		cout << Div(10, 0) << endl;
	}
	catch (char)//按类型捕获
	{
		cout << "你看着办." << endl;
	}

	catch (int)
	{
		cout << "喝的高兴." << endl;
	}

	catch (double)
	{
		cout << "再点根烟." << endl;
	}

	catch(...)//捕获想不到的类型 等效于default
	{
		cout << "有我担着" << endl;
	}
}

void main()
{
	test01();
	system("pause");
}
异常规范:
1. 异常规格说明的目的是为了让函数使用者知道该函数可能抛出的异常有哪些。 可以在函数的后面接
throw( 类型 ) ,列出这个函数可能抛掷的所有异常类型。
2. 函数的后面接 throw() ,表示函数不抛异常。
3. 若无异常接口声明,则此函数可以抛掷任何类型的异常
#include<iostream>
using namespace std;

#include<iostream>
#include<assert.h>
//异常
using namespace std;

int Div(int a, int b) throw(int,double)//加了throw()表示这个函数不能抛出异常 抛出(int,double)指定类型异常
{
	/*if (b == 0)
	{
		return 0;
	}*/
	//assert(b != 0);//assert只能在debug版本下使用
	double c = 1.0;
	string s = "";
	if (b == 0)
		throw(s);//抛出类型而非抛出值
	return a / b;
}

void test01()
{
	try
	{
		cout << Div(10, 0) << endl;
	}
	catch (char)//按类型捕获
	{
		cout << "你看着办." << endl;
	}

	catch (int)
	{
		cout << "喝的高兴." << endl;
	}

	catch (double)
	{
		cout << "再点根烟." << endl;
	}

	catch (...)//捕获想不到的类型 等效于default
	{
		cout << "有我担着" << endl;
	}
}

void main()
{
	test01();
	system("pause");
}

异常的重新抛出:

        有可能单个的 catch 不能完全处理一个异常,在进行一些校正处理以后,希望再交给更外层的调用链函数来处理, catch 则可以通过重新抛出将异常传递给更上层的函数进行处理。
#include<iostream>
using namespace std;

double Division(int a, int b)
{
	 当b == 0时抛出异常
	if (b == 0)
	{
		throw string{ "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)
	catch(const string& s)
	{
		cout << s<< endl;
	}
	return 0;
}

C++标准库异常的使用举例:

#include<iostream>
#include<atomic>
using namespace std;

void Out_Of_Memory()
{
	cout << "Out_Of_Memory" << endl;
	exit(1);
}

void main()
{
	//set_new_handler(Out_Of_Memory);
	int* p=nullptr;
	try
	{
		 p = new int[536870911];
	}

	catch (const bad_alloc& e)
	{
		cout << e.what() << endl;
	}
	delete[] p;
	cout << "Main End" << endl;
}