C++异常和断言

C++异常

  • 异常的理念看似有前途,但实际的使用效果并不好。
  • 编程社区达成的一致意见是,最好不要使用这项功能。
  • C++98引入异常规范,C++11已弃用。
    例如:我们输入1时抛出异常。
#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>//ifstream 类需要包含的头文件
#include<string>
using namespace std;
int main() {
	try {
		//可能抛出的异常的代码
		int ii = 0;
		cout << "我是一只小小鸟?(1-傻傻鸟;2-小小鸟)";
		cin >> ii;
		if (ii == 1) throw "不好,有人说我是一只傻傻鸟";//throw抛出异常
		cout << "我不是一只傻傻鸟,欧耶\n";
	}
	catch (...) {//不管什么异常,都在这里处理
		cout << "捕捉到异常,具体不管什么异常\n";
	}

	cout << "程序继续使用....\n";//执行完try..catch...后,将继续指向程序中其他代码
	return 0;
}
#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>//ifstream 类需要包含的头文件
#include<string>
using namespace std;
int main() {
	try {
		//可能抛出的异常的代码
		int ii = 0;
		cout << "我是一只小小鸟?(1-傻傻鸟;2-小小鸟)";
		cin >> ii;
		if (ii == 1) throw "不好,有人说我是一只傻傻鸟";//throw抛出const char *类型异常
		if (ii == 2) throw ii;//throw抛出int类型异常
		if (ii == 3) throw string("不好,有人说我是一只傻傻鸟.");//throw抛出string类型异常
		
		cout << "我不是一只傻傻鸟,欧耶\n";
	}
	catch (int ii) {
		cout << "异常的类型是int=" << ii << endl;
	}
	catch (const char* ss) {
		cout << "异常的类型是const char*=" << ss << endl;
	}
	catch (string str) {
		cout << "异常的类型是string" << str << endl;
	}

	cout << "程序继续使用....\n";//执行完try..catch...后,将继续指向程序中其他代码
	return 0;
}

try语句块中,如果没有发生异常,执行完try语句块中的代码后,将继续执行try语句块之后的
代码;如果发生了异常,用throw抛出异常对象,异常对象的类型决定了应该匹配到哪个catch语句
块,如果没有匹配到catch语句块,程序将调用abort()函数中止
如果try语句块中用throw抛出异常对象,并且匹配到了catch语句块,执行完catch语句块中的
代码后,将继续执行catch语句块之后的代码,不会回到try语句块中。

如何避免异常

异常规范

C++98标准提出了异常规范,目的是为了让使用者知道函数可能会引发哪些异常。

void func1() throw(A, B,C);//表示该函数可能会抛出A、B、C类型的异常。
void func2() throw();//表示该函数不会抛出异常。
void func3();//该函数不符合C++98的异常规范。

C++11标准弃用了异常规范,使用新增的关键字noexcept指出函数不会引发异常。

void func4() noexcept;//该函数不会抛出异常。

在实际开发中,大部分程序员懒得在函数后面加noexcept,弃用异常已是共识,没必要多此一举。
关键字noexcept也可以用作运算符,判断表达试(操作数)是否可能引发异常;如果表达式可能引发异常,则返回false否则返回true。
在这里插入图片描述
对于下面的代码:

#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>//ifstream 类需要包含的头文件
#include<string>
using namespace std;
int main() {
	
	//分配一大块内存
	double* ptr = nullptr;
	try {
		ptr = new double[100000000000];
	}
	catch (bad_alloc& e) {
		cout << "分配内存失败\n";
	}
	return 0;
}

我们使用这种也行:

#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>//ifstream 类需要包含的头文件
#include<string>
using namespace std;
int main() {
	
	//分配一大块内存
	double* ptr = nullptr;
	ptr = new(std::nothrow) double[100000000000];
	if (ptr == nullptr)cout << "分配内存失败\n";
	//其他处理业务代码
	if (ptr != nullptr) delete[]ptr;
	return 0;
}

我们在实际开发中经常用这种,这种比较简单。

重点关注的异常

  1. std::bad_alloc
    如果内存不足,调用new会产生异常,导致程序中止;如果在new关键字后面加(std:nothrow)选项,则返回nullptr,不会产生异常。

  2. dynamic_cast
    dynamic_cast可以用于引用,但是,没有与空指针对应的引用值,如果转换请求不正确,会出现
    td::bad_cast异常。

  3. std:bad_typeid
    假设有表达式typeid(*ptr),当ptr是空指针时,如果ptr是多态的类型,将引发std:bad_typeid异常。

逻辑错误异常

程序的逻辑错误产生的异常std::logic_error,通过合理的编程可以避免。

#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>//ifstream 类需要包含的头文件
#include<string>
using namespace std;
int main() {
	
	try {
		vector<int>vv = { 1,2,3 };//容器vv中只有三个元素
		vv.at(3) = 5;//将引发out_of_range异常
	}
	catch (out_of_range) {
		cout << "出现out_of_range异常\n";
	}
	return 0;
}
#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>//ifstream 类需要包含的头文件
#include<string>
using namespace std;
int main() {
	//string str="123";//不会抛出异常
	//string str="";//将抛出Invalid_argument异常
	string str = "21212312312313123123123";//将抛出out_of_range异常
	try {
		int x = stoi(str);//将string字符串转化为整数
		cout << "x=" << x << endl;
	}
	catch (invalid_argument&) {
		cout << "出现invalid_argument异常\n";
	}
	catch (out_of_range&) {
		cout << "出现out_of_range异常\n";
	}
	return 0;
}

这个逻辑异常应该有程序员自己解决

C++断言

断言(assertion)是一种常用的编程手段,用于排除程序中不应该出现的逻辑错误。
使用断言需要包含头文件<cassert><assert.h>,头文件中提供了带参数的宏assert,用于程序
在运行时进行断言。

语法:assert(表达式);

断言就是判断(表达式)的值,如果为0(false),程序将调用abort()函数中止,如果为非0 (true),程序继续执行。
断言可以提高程序的可读性,帮助程序员定位违反了某些前提条件的错误。
例如:

#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>//ifstream 类需要包含的头文件
#include<string>
#include<cassert>
using namespace std;
void copydata(void* ptr1, void* ptr2) {//把ptr2中的数据复制到ptr1中。
	assert(ptr1 && ptr2);//断言ptr1和ptr2都不会为空
	cout << "继续执行复制数据的代码....\n";

}
int main() {
	int ii = 0, jj = 0;
	copydata(&ii, &jj);//把ptr2中的数据复制到ptr1中。
	return 0;
}

这个是正常的,就是把jj中的数据复制到ii中。
假如是这个代码:

#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>//ifstream 类需要包含的头文件
#include<string>
#include<cassert>
using namespace std;
void copydata(void* ptr1, void* ptr2) {//把ptr2中的数据复制到ptr1中。
	assert(ptr1 && ptr2);//断言ptr1和ptr2都不会为空
	cout << "继续执行复制数据的代码....\n";

}
int main() {
	int ii = 0, jj = 0;
	copydata(nullptr, &jj);//把ptr2中的数据复制到ptr1中。
	return 0;
}

在这里插入图片描述
注意:

  • 断言用于处理程序中不应该发生的错误,而非逻辑上可能会发生的错误。
  • 不要把需要执行的代码放到断言的表达式中。
  • 断言的代码一般放在函数/成员函数的第一行,表达式多为函数的形参,主要用于判断参数是否参数的合法性。

C++11 静态断言

assert宏是运行时断言,在程序运行的时候才能起作用。
C++11新增了静态断言static_assert,用于在编译时检查源代码。
使用静态断言不需要包含头文件。
语法: static_assert(常量表达式,提示信息);
用途: 在编译的时候,判断常量表达式的值,如果是0-false,那么编译失败,否则显示提示信息。
注意: static_assert的第一个参数是常量表达式。而assert 的表达式既可以是常量,也可以是变
量。

#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>//ifstream 类需要包含的头文件
#include<string>
#include<cassert>
using namespace std;
void copydata(void* ptr1, void* ptr2) {//把ptr2中的数据复制到ptr1中。
	assert(ptr1 && ptr2);//断言ptr1和ptr2都不会为空
	cout << "继续执行复制数据的代码....\n";

}
int main() {
	const int ii = 1, jj = 0;
	assert(ii);
	static_assert(ii, "ii的值不合法。");
	//copydata(nullptr, &jj);//把ptr2中的数据复制到ptr1中。
	return 0;
}
  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值