当前位置:网站首页>system_error错误处理库学习
system_error错误处理库学习
2022-08-02 09:07:00 【班公湖里洗过脚】
在程序开发时,我们有时遇到程序出错了,但不知道具体的原因,莫名其妙的就崩溃了,其实标准库提供了一个系统错误的库,我们可以使用这个system_error库来了解错误的提示。它不仅提供错误码code(),还提供错误类别category(),错误信息message()等相关接口。
成员函数
| 构造一个 error_code (公开成员函数) | |
| 赋值为另一 error_code (公开成员函数) | |
| 赋值为另一 error_code (公开成员函数) | |
修改器 | |
| 设 error_code 为 system_category 中的值 0 (公开成员函数) | |
观察器 | |
| 获得 error_code 的值 (公开成员函数) | |
| 获得此 error_code 的 error_category (公开成员函数) | |
| 获得此 error_code 的 error_condition (公开成员函数) | |
| 获得此 error_code 的解释性字符串 (公开成员函数) | |
| 检查值是否非零 (公开成员函数) | |
下面是具体的示例:
//error_code observers: value, category and message
#include <iostream> //std::cout, std::ios
#include <system_error> //std::system_error
#include <fstream> //std::ifstream
#include <string> //std::string
using std::cout;
using std::ios;
using std::endl;
using std::system_error;
using std::ifstream;
using std::string;
int main()
{
std::error_code code;
cout << "Code: " << code.value() << endl;
cout << "Category: " << code.category().name() << endl;
cout << "Message: " << code.message() << endl;
cout << "default_error_condition: " << code.default_error_condition().message() << endl;
cout << "bool: " << code.operator bool() << endl;
cout << "Hello World!" << endl;
return 0;
}
运行结果:

上面是创建一个error_code 对象,程序正常运行,根据上面的结果可以看没程序正常没有错误。
下面我们来看一个异常的程序。
//error_code observers: value, category and message
#include <iostream> //std::cout, std::ios
#include <system_error> //std::system_error
#include <fstream> //std::ifstream
#include <string> //std::string
using std::cout;
using std::ios;
using std::endl;
using std::system_error;
using std::ifstream;
using std::string;
int main()
{
ifstream is;
is.exceptions(std::ios::failbit);
try {
is.open("unexistent.txt");
} catch (const std::system_error &e) {
cout << "Exception caught (system_error):\n" << endl;
cout << "Error: " << e.what() << endl;
cout << "Code: " << e.code().value() << endl;
cout << "Category: " << e.code().category().name() << endl;
cout << "Message: " << e.code().message() << endl;
cout << "default_error_condition: " << e.code().default_error_condition().message() << endl;
cout << "bool: " << e.code().operator bool() << endl;
cout << endl;
}
cout << "Hello World!" << endl;
return 0;
}
运行结果:

打开一个不存在的文件,出现了异常,从上面的异常提示我们知道这是输入输出流错误。
接着,我们把这个错误对象赋给第一个程序创建的一个空的错误类对象。查看他的信息。
//error_code observers: value, category and message
#include <iostream> //std::cout, std::ios
#include <system_error> //std::system_error
#include <fstream> //std::ifstream
#include <string> //std::string
using std::cout;
using std::ios;
using std::endl;
using std::system_error;
using std::ifstream;
using std::string;
int main()
{
std::error_code code;
cout << "Code: " << code.value() << endl;
cout << "Category: " << code.category().name() << endl;
cout << "Message: " << code.message() << endl;
cout << "default_error_condition: " << code.default_error_condition().message() << endl;
cout << "bool: " << code.operator bool() << endl;
cout << endl;
ifstream is;
is.exceptions(std::ios::failbit);
try {
is.open("unexistent.txt");
} catch (const std::system_error &e) {
cout << "Exception caught (system_error):\n" << endl;
cout << "Error: " << e.what() << endl;
cout << "Code: " << e.code().value() << endl;
cout << "Category: " << e.code().category().name() << endl;
cout << "Message: " << e.code().message() << endl;
cout << "default_error_condition: " << e.code().default_error_condition().message() << endl;
cout << "bool: " << e.code().operator bool() << endl;
cout << endl;
// operator= 操作
code = e.code();
cout << "Code: " << code.value() << endl;
cout << "Category: " << code.category().name() << endl;
cout << "Message: " << code.message() << endl;
cout << "default_error_condition: " << code.default_error_condition().message() << endl;
cout << "bool: " << code.operator bool() << endl;
cout << endl;
}
cout << "Hello World!" << endl;
return 0;
}
运行结果:

e和code两个错误对象的信息相同,接下来再调用clear函数,把信息内容清空
//error_code observers: value, category and message
#include <iostream> //std::cout, std::ios
#include <system_error> //std::system_error
#include <fstream> //std::ifstream
#include <string> //std::string
using std::cout;
using std::ios;
using std::endl;
using std::system_error;
using std::ifstream;
using std::string;
int main()
{
std::error_code code;
cout << "Code: " << code.value() << endl;
cout << "Category: " << code.category().name() << endl;
cout << "Message: " << code.message() << endl;
cout << "default_error_condition: " << code.default_error_condition().message() << endl;
cout << "bool: " << code.operator bool() << endl;
cout << endl;
ifstream is;
is.exceptions(std::ios::failbit);
try {
is.open("unexistent.txt");
} catch (const std::system_error &e) {
cout << "Exception caught (system_error):\n" << endl;
cout << "Error: " << e.what() << endl;
cout << "Code: " << e.code().value() << endl;
cout << "Category: " << e.code().category().name() << endl;
cout << "Message: " << e.code().message() << endl;
cout << "default_error_condition: " << e.code().default_error_condition().message() << endl;
cout << "bool: " << e.code().operator bool() << endl;
cout << endl;
// operator= 操作
code = e.code();
cout << "Code: " << code.value() << endl;
cout << "Category: " << code.category().name() << endl;
cout << "Message: " << code.message() << endl;
cout << "default_error_condition: " << code.default_error_condition().message() << endl;
cout << "bool: " << code.operator bool() << endl;
cout << endl;
//调用clear()
code.clear();
cout << "Code: " << code.value() << endl;
cout << "Category: " << code.category().name() << endl;
cout << "Message: " << code.message() << endl;
cout << "default_error_condition: " << code.default_error_condition().message() << endl;
cout << "bool: " << code.operator bool() << endl;
cout << endl;
}
cout << "Hello World!" << endl;
return 0;
}
运行结果:

code对象又恢复到初始状态了。
我们也可以对错误码对像进行hash:
//error_code observers: value, category and message
#include <iostream> //std::cout, std::ios
#include <system_error> //std::system_error
#include <fstream> //std::ifstream
#include <string> //std::string
using std::cout;
using std::ios;
using std::endl;
using std::system_error;
using std::ifstream;
using std::string;
int main()
{
ifstream is;
is.exceptions(std::ios::failbit);
try {
is.open("unexistent.txt");
} catch (const std::system_error &e) {
cout << "Exception caught (system_error):\n" << endl;
cout << "Error: " << e.what() << endl;
cout << "Code: " << e.code().value() << endl;
cout << "Category: " << e.code().category().name() << endl;
cout << "Message: " << e.code().message() << endl;
cout << "default_error_condition: " << e.code().default_error_condition().message() << endl;
cout << "bool: " << e.code().operator bool() << endl;
cout << endl;
std::hash<std::error_code> hashErrorCode;
cout << "hash(error_code): " << hashErrorCode(e.code()) << endl;
}
cout << "Hello World!" << endl;
return 0;
}
运行结果:

hash这是一个结构体模板,它不仅可以对错误码对象进行hash也可以对数值进行hash,比如int, long long, float, double,string等,下面来看一个示例:
//error_code observers: value, category and message
#include <iostream> //std::cout, std::ios
#include <system_error> //std::system_error
#include <fstream> //std::ifstream
#include <string> //std::string
using std::cout;
using std::ios;
using std::endl;
using std::system_error;
using std::ifstream;
using std::string;
int main()
{
ifstream is;
is.exceptions(std::ios::failbit);
try {
is.open("unexistent.txt");
} catch (const std::system_error &e) {
cout << "Exception caught (system_error):\n" << endl;
cout << "Error: " << e.what() << endl;
cout << "Code: " << e.code().value() << endl;
cout << "Category: " << e.code().category().name() << endl;
cout << "Message: " << e.code().message() << endl;
cout << "default_error_condition: " << e.code().default_error_condition().message() << endl;
cout << "bool: " << e.code().operator bool() << endl;
cout << endl;
std::hash<std::error_code> hashErrorCode;
cout << "hash(error_code): " << hashErrorCode(e.code()) << endl;
string str = "hello world!";
std::hash<string> hashStr;
cout << "hash(string): " << hashStr(str) << endl;
int n = 100;
std::hash<int> hashN;
cout << "hash(int): " << hashN(n) << endl;
long long ln = 100;
std::hash<long long> hashLn;
cout << "hash(long long): " << hashLn(ln) << endl;
double dou = 100;
std::hash<double> hashD;
cout << "hash(double): " << hashD(dou) << endl;
float f = 100;
std::hash<float> hashF;
cout << "hash(float): " << hashF(f) << endl;
}
cout << "Hello World!" << endl;
return 0;
}
运行结果:

参考:
边栏推荐
- js函数防抖和函数节流及其使用场景
- Nodejs3day(express简介,express创建基本Web服务器,托管静态资源,nodemon下载及出现的问题,中间件,编写GET,POST,JSONP接口)
- Overview of Edge Computing Open Source Projects
- pycharm的基本使用教程(1)
- Spend 2 hours a day to make up for Tencent T8, play 688 pages of SSM framework and Redis, and successfully land on Meituan
- 新起点丨MeterSphere开源持续测试平台v2.0发布
- spark:页面单跳转换率统计(案例)
- C语言基础_共用体
- 【论文阅读】Distilling the Knowledge in a Neural Network
- EdrawMax Crack,多合一的图表应用程序
猜你喜欢

PyQt5(一) PyQt5安装及配置,从文件夹读取图片并显示,模拟生成素描图像

Bigder:41/100生产bug有哪些分类

How to use postman

SAP 云平台上一种 Low Code Development(低代码开发)解决方案

线程池的使用及ThreadPoolExecutor源码分析

Spend 2 hours a day to make up for Tencent T8, play 688 pages of SSM framework and Redis, and successfully land on Meituan

深度学习汇报(4)

Jenkins--基础--07--Blue Ocean

Redis数据结构

被报表需求逼疯的银行数据人,是时候放弃用Excel做报表了
随机推荐
HCIA动态主机配置协议实验(dhcp)
PyQt5 (a) PyQt5 installation and configuration, read from the folder and display images, simulation to generate the sketch image
leetcode:81. 搜索旋转排序数组 II
MySQL安装与卸载详细教程
WebGPU 导入[1] - 入门常见问题与个人分享
腾讯T8架构师,教你学中小研发团队架构实践PDF,高级架构师捷径
向量组的线性相关性
The god-level Alibaba "high concurrency" tutorial "basic + actual combat + source code + interview + architecture"
location对象,navigator对象,history对象学习
postman使用方法
pycharm的基本使用教程(1)
openpyxl 单元格合并
自定义View实现波浪荡漾效果
AutoJs学习-AES加解密
spark:热门品类中每个品类活跃的SessionID统计TOP10(案例)
单机部署flink,创建oracle19c rac的连接表时报错 ORA-12505 ,怎么回事?
膜拜,Alibaba分布式系统开发与核心原理解析手册
leetcode:639. 解码方法 II
编程与哲学(2)——输出是为了更好的输入
不用Swagger,那我用啥?