C++ 异常

一,定义

异常是程序在执行期间产生的问题,C++ 异常是指程序在运行过程中产生可预料的执行分支,如除0操作,数组访问越界、要打开的文件不存在。不同于bug,bug是指程序中的错误,是不被预期的运行方式。如野指针、堆空间使用结束未释放。

以下情况可产生异常:

  • 做除法的时候除数为 0;
  • 用户输入年龄时输入了一个负数;
  • 用 new 运算符动态分配空间时,空间不够导致无法分配;
  • 访问数组元素时,下标越界;
  • 打开文件读取时,文件不存在。

 

二,处理异常

所谓“处理”,可以是给出错误提示信息,然后让程序沿一条不会出错的路径继续执行;也可能是不得不结束程序,但在结束前做一些必要的工作,如将内存中的数据写入文件、关闭打开的文件、释放动态分配的内存空间等。
 

C++ 通过 throw 语句和 try...catch 语句实现对异常的处理。throw 语句的语法如下:

throw  表达式;

该语句拋出一个异常。异常是一个表达式,其值的类型可以是基本类型,也可以是类。

try...catch 语句的语法如下:

try {
    语句组
}
catch(异常类型) {
    异常处理代码
}
...
catch(异常类型) {
    异常处理代码
}

catch 可以有多个,但至少要有一个。

try...catch 语句的执行过程是:

  • 执行 try 块中的语句,如果执行的过程中没有异常拋出,那么执行完后就执行最后一个 catch 块后面的语句,所有 catch 块中的语句都不会被执行;
  • 如果 try 块执行的过程中拋出了异常,那么拋出异常后立即跳转到第一个“异常类型”和拋出的异常类型匹配的 catch 块中执行(称作异常被该 catch 块“捕获”),执行完后再跳转到最后一个 catch 块后面继续执行。

例1:

int main()
{
	double m,n;
	cin >> m>> n;

	try
	{
		if (n == 0)
			throw 2; //int 型
		else
			cout << m / n << endl;
	}
	catch (double d)
	{
		cout << "捕获(double)" << d << endl;
	}
	catch (int i) {
		cout << "捕获(int)" << i << endl;
	}



	system("pause");
	return 0;
}

当n为0时,抛出异常 2,被catch(int i ) 捕获。

 

例2:

int main()
{
	double m,n;
	cin >> m>> n;

	try
	{
		if (n == 0)
			throw 2.0;
		else
			cout << m / n << endl;
	}
	catch (double d)
	{
		cout << "捕获(double)" << d << endl;
	}
	catch (int i) {
		cout << "捕获(int)" << i << endl;
	}

	system("pause");
	return 0;
}

当n为0时,抛出异常 2.0,被catch(double) 捕获。

 

例3:

int main()
{
	double m,n;
	cin >> m>> n;

	try
	{
		if (n == 0)
			throw 2.0;
		else
			cout << m / n << endl;
	}
	catch (char d)
	{
		cout << "捕获(char)" << d << endl;
	}
	catch (int i) {
		cout << "捕获(int)" << i << endl;
	}

	system("pause");
	return 0;
}

 当n为0时,抛出异常 2.0,与现有catch() 类型不匹配,无法捕获,程序终止。

 

例4:

int main()
{
	double m,n;
	cin >> m>> n;

	try
	{
		if (n == 0)
			throw 2.0;
		else
			cout << m / n << endl;
	}
	catch (char d)
	{
		cout << "捕获(char)" << d << endl;
	}
	catch (int i) {
		cout << "捕获(int)" << i << endl;
	}
	catch (...) { //可捕获任何异常
		cout << "捕获" << endl;
	}

	system("pause");
	return 0;
}

catch(...) {
    ...

可捕获任何异常。

三,自定义异常

struct MyException:public exception
{
	const char* what() const throw() {
		return "my C++ exception";
	}
};

int main()
{
	double m,n;
	cin >> m>> n;

	try
	{
		if (n == 0)
			throw MyException();
		else
			cout << m / n << endl;
	}
	catch (MyException& e)
	{
		cout << "捕获(自定义异常)" << e.what() << endl;
	}
	

	system("pause");
	return 0;
}

四,C++ 标准的异常

异常描述
std::exception该异常是所有标准 C++ 异常的父类。
std::bad_alloc该异常可以通过 new 抛出。
std::bad_cast该异常可以通过 dynamic_cast 抛出。
std::bad_exception这在处理 C++ 程序中无法预期的异常时非常有用。
std::bad_typeid该异常可以通过 typeid 抛出。
std::logic_error理论上可以通过读取代码来检测到的异常。
std::domain_error当使用了一个无效的数学域时,会抛出该异常。
std::invalid_argument当使用了无效的参数时,会抛出该异常。
std::length_error当创建了太长的 std::string 时,会抛出该异常。
std::out_of_range该异常可以通过方法抛出,例如 std::vector 和 std::bitset<>::operator[]()。
std::runtime_error理论上不可以通过读取代码来检测到的异常。
std::overflow_error当发生数学上溢时,会抛出该异常。
std::range_error当尝试存储超出范围的值时,会抛出该异常。
std::underflow_error当发生数学下溢时,会抛出该异常。

 

 

参考:

https://www.runoob.com/cplusplus/cpp-exceptions-handling.html

https://blog.51cto.com/9291927/2164586

http://c.biancheng.net/view/422.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

土拨鼠不是老鼠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值