C++异常处理,标准库中异常

本文会围绕以下六个知识点展开介绍 标准库中的异常在最下边
No1.异常处理机制
问题的检测和问题的处理分开
A函数发生问题,不做处理,抛出异常,放到A函数调用者B函数处理,或者B调用者C函数处理(因为只要出现异常,本函数后边的内容就不会执行了,所有只能在其他地方处理),等效于问题的延期处理。
异常是什么?任何东西都可以当作异常处理, 错误只是异常的一种
No2. 异常处理用的几个关键字
2.1 throw 抛出异常
2.2 try 捕获异常
2.3 catch 处理异常
注意问题:
try必须要跟catch一起使用
try 和 catch 各自的{ } 不能省略
当抛出异常,不做处理的时候,会调用abort程序
一个try可以对应多个catch,但是只能执行一个catch
No3.删减符:…任何东西都能捕获处理
No4.不存在异常的描述,throw() 或者 noexcept
注意点:当用noexcept描述,偏要抛出异常,将会引发中断
No5.异常处理中的传参操作
5.1基本数据传参
5.2抛出自定义类型,传参,通常把抛出对象的类自定义异常类


#include<iostream>
using namespace std;
int divisor(int a, int b) {
	if (b == 0)
		throw b;  //抛出B
	return a / b;
}

void printResult() {
	divisor(1, 0);
	cout << "子函数结束" << endl;//上一句会引发异常,所以这句不执行
}

void testDeletOperator() {
	try {
		divisor(1, 0);
	}
	catch (...) {    
		cout << "任何抛出的都可以处理!\n" << endl;
	}
}

int Max(int a, int b) throw() {
	return a > b ? a : b;
}
int Min(int a, int b) noexcept {
	return a < b ? a : b;
}
void transferParameters() {
	try {
		divisor(1, 0);
		cout << "存在异常这里不会执行!\n" << endl;
	}
	catch (int num) {//隐藏了一个传参过程,int num=抛出来的值
		cout << "num" << num << endl;
		cout << "除数不能为0" << endl;
	}
}

class StackEmpty {
public:
	StackEmpty(string info=""):info(info){}
	const char* what() const {
		return info.c_str();
	}
protected:
	string info;
};
//假装是一个出栈操作
int size = 0;
void pop() {
	if (::size == 0) {  //加上::表示是一个全局的size
		throw StackEmpty("栈为空!");
	}
	cout << "正常出栈" << endl;
}

void testUserException() {
	try {
		pop();
	}
	catch (StackEmpty& temp) {
		cout << temp.what();
	}
}
int main() {
	try {
		printResult();
	}
	catch (int) {
		cout << "除数不能为0!\n" << endl;
	}
	catch (double) { //一个try 可以对应多个catch,但只能执行一个catch
		cout << "double";
	}
	testDeletOperator();
	transferParameters();
	testUserException();
}

No6.标准库的异常处理:
标准库中异常:了解一下,下次看到这样的错误,要大致知道在那些地方找错误。
bad_alloc:内存申请失败返回的异常,new
bad_cast :dynamic_cast失败,下行转换失败
bad_typeid:typeinfo对空指针操作的时候
bad_exception:不存在异常的函数抛出异常
ios_base::failure:C++输入输出流执行发生错误
runtime_error: 运行时错误
underflow_error:向下溢出 抛出异常
overflow_error:向上溢出 抛出异常
range_error:作用域错误,抛出异常
logic_error:逻辑错误
out_of_range:参数值不在允许的范围内
invalid_argument:函数传参出现问题
domain_error: 执行程序的决定条件不足

#include<iostream>
#include<exception>
using namespace std;
/*
	自定义异常类
	No1.继承标准库中的异常类,自己去写构造函数和拷贝构造函数
	No2.重写what方法,或者重新初始化_Data.what数据成员
*/

//No1.继承 ,自己写构造
class Out_Range :public exception {
public:
	Out_Range(const string& message) :exception(message.c_str()) {}
	Out_Range(const char* message):exception(message){}
};
void printArray(int arr[], int len) {
	int i = -1;
	if (i<0 || i>len) {
		throw  Out_Range("数组下标越界了!!");
	}
	cout << arr[i];
}

//No2.第二种方法,重写
class Error :public exception {
public:
	const char* what() const override {//override表示重写父类函数
		return "嘻嘻,江涛帅哥!!";
	}
};
void printvalue() {
	throw Error();
}

void newMemory() {
	while (1) {
		int* p = new int[1000];
	}
}
int main() {
	int arr[] = { 1,2,3,4 };
	try {
		printArray(arr, 4);
	}
	catch(Out_Range& result){
		cout << result.what();
	}
	try {
		printvalue();
	}
	catch(Error& result){
		cout << result.what();
	}


	try {
		newMemory();
	}
	catch (bad_alloc& result) {
		cout << result.what();
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值