C++学习Day09之异常的基本用法


前言

在 C++ 中,异常处理是一种用于处理程序运行时出现的异常情况的机制。当程序执行过程中发生异常时,可以使用异常处理机制来捕获并处理这些异常,以避免程序崩溃或出现不可预料的行为。

一、程序及输出

1.1 C语言处理异常的缺陷

C语言处理异常 有缺陷:返回值不统一,返回值只有一个,无法区分是结果还是异常

#include<iostream>
using namespace std;
#include <string>
int myDivision(int a , int b)
{
	if ( b == 0)
	{
		return -1;
	}

	return a / b;
}

void test01()
{
	int a = 10;
	int b = 0;

	//C语言处理异常 有缺陷:返回值不统一,返回值只有一个,无法区分是结果还是异常
	int ret =myDivision(a, b);
	if ( ret == -1)
	{
		cout << "异常" << endl;
	}
}
int main(){

	test01();
	system("pause");
	return EXIT_SUCCESS;
}

输出:
在这里插入图片描述
但如果a10,b=-10,结果仍为异常

void test01()
{
	int a = 10;
	int b = -10;

	//C语言处理异常 有缺陷:返回值不统一,返回值只有一个,无法区分是结果还是异常
	int ret =myDivision(a, b);
	if ( ret == -1)
	{
		cout << "异常" << endl;
	}

输出:
在这里插入图片描述

1.2 c++处理异常

在 C++ 中,throw 语句用于抛出异常。当使用 throw 抛出一个整数值时,实际上抛出的是一个 int 类型的异常。这种异常可以被 catch 块捕获,并进行处理。同理其他类型也可以捕获。

1.2.1 基本使用

#include<iostream>
using namespace std;
#include <string>
int myDivision(int a , int b)
{
	if ( b == 0)
	{
		throw 1; //抛出int类型的异常
	}
	return a / b;
}
void test01()
{
	int a = 10;
	int b = 0;

	try
	{
		myDivision(a, b);
	}
	catch (int)
	{
		cout << "int类型异常捕获" << endl;
	}
}
int main(){

	test01();
	system("pause");
	return EXIT_SUCCESS;
}

输出:
在这里插入图片描述

1.2.2 捕获其他类型 catch(…)

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

int myDivision(int a , int b)
{
	if ( b == 0)
	{
		string str = "abc";
		throw str;
	}

	return a / b;
}

void test01()
{
	int a = 10;
	int b = 0;

	try
	{
		myDivision(a, b);
	}
	catch (int)
	{
		cout << "int类型异常捕获" << endl;
	}
	catch (char)
	{
		cout << "char类型异常捕获" << endl;
	}
	catch (double)
	{
		cout << "double类型异常捕获" << endl;
	}
	catch (...)
	{
		cout << "其他类型异常捕获" << endl;
	}
}
int main(){

	test01();
	system("pause");
	return EXIT_SUCCESS;
}

输出:
在这里插入图片描述

1.2.3 异常不处理,继续向上抛出

异常不处理,使用throw继续抛出.

异常必须有函数进行处理,如果没有任何处理,程序自动调用 terminate 函数,让程序中断

#include<iostream>
using namespace std;
#include <string>
int myDivision(int a , int b)
{
	if ( b == 0)
	{
		throw 3.14; //抛出double类型的异常
	}

	return a / b;
}
void test01()
{
	int a = 10;
	int b = 0;

	try
	{
		myDivision(a, b);
	}
	catch (int)
	{
		cout << "int类型异常捕获" << endl;
	}
	catch (char)
	{
		cout << "char类型异常捕获" << endl;
	}
	catch (double)
	{
		//捕获到了异常,但是不想处理,继续向上抛出这个异常
		//异常必须有函数进行处理,如果没有任何处理,程序自动调用 terminate 函数,让程序中断
		throw;
		cout << "double类型异常捕获" << endl;
	}
	catch (...)
	{
		cout << "其他类型异常捕获" << endl;
	}
	
}
int main(){
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

运行报错:
在这里插入图片描述

正确做法:在外界做异常处理。

......省略

int main(){

	try
	{
		test01();
	}
	catch (double)
	{
		cout << "double函数中 double类型异常捕获" << endl;
	}
	catch (...)
	{
		cout << "main函数中 其他类型异常捕获" << endl;
	}
	system("pause");
	return EXIT_SUCCESS;
}

输出:
在这里插入图片描述

1.2.4 异常可以是自定义数据类型

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

class MyException
{
public:
	void printError()
	{
		cout << "我自己的异常" << endl;
	}
};
int myDivision(int a , int b)
{
	if ( b == 0)
	{
		throw MyException(); //抛出 MyException的匿名对象
	}

	return a / b;
}
void test01()
{
	int a = 10;
	int b = 0;

	try
	{
		myDivision(a, b);
	}
	catch (int)
	{
		cout << "int类型异常捕获" << endl;
	}
	catch (char)
	{
		cout << "char类型异常捕获" << endl;
	}
	catch (double)
	{
		cout << "double类型异常捕获" << endl;
	}
	catch (MyException e)
	{
		e.printError();
	}
	catch (...)
	{
		cout << "其他类型异常捕获" << endl;
	}
}
int main(){
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

输出:
在这里插入图片描述

1.2.5 栈解旋

从try代码块开始,到throw抛出异常之前,所有栈上的数据都会被释放掉,
释放的顺序和创建顺序相反的,这个过程我们称为栈解旋

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

class Person
{
public:
	Person()
	{
		cout << "Person的默认构造函数调用" << endl;
	}
	~Person()
	{
		cout << "Person的析构函数调用" << endl;
	}
};

class MyException
{
public:
	void printError()
	{
		cout << "我自己的异常" << endl;
	}
};
int myDivision(int a , int b)
{
	if ( b == 0)
	{
		Person p1;
		Person p2;
		throw MyException(); //抛出 MyException的匿名对象
	}

	return a / b;
}
void test01()
{
	int a = 10;
	int b = 0;

	try
	{
		myDivision(a, b);
	}
	catch (MyException e)
	{
		e.printError();
	}
	catch (...)
	{
		cout << "其他类型异常捕获" << endl;
	}
}
int main(){
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

输出:
在这里插入图片描述


二、分析与总结

C语言处理异常
有缺陷:返回值不统一,返回值只有一个,无法区分是结果还是异常
c++异常处理
1 try throw catch
2 可以出现异常的代码 放到 try块
3 利用throw抛出异常
4 利用catch捕获异常
5 catch( 类型) 如果想捕获其他类型 catch(…)
6 如果捕获到的异常不想处理,而继续向上抛出,利用 throw
7 异常必须有函数进行处理,如果都不去处理,程序自动调用 terminate函数,中断掉
8 异常可以是自定义数据类型
9 栈解旋
从try代码块开始,到throw抛出异常之前,所有栈上的数据都会被释放掉,释放的顺序和创建顺序相反的,这个过程我们称为栈解旋

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值