• system_error错误处理库学习


    在程序开发时,我们有时遇到程序出错了,但不知道具体的原因,莫名其妙的就崩溃了,其实标准库提供了一个系统错误的库,我们可以使用这个system_error库来了解错误的提示。它不仅提供错误码code(),还提供错误类别category(),错误信息message()等相关接口。

    成员函数

    (构造函数)

    构造一个 error_code
    (公开成员函数)

    operator=

    赋值为另一 error_code
    (公开成员函数)

    assign

    赋值为另一 error_code
    (公开成员函数)

    修改器

    clear

    设 error_code 为 system_category 中的值 0
    (公开成员函数)

    观察器

    value

    获得 error_code 的值
    (公开成员函数)

    category

    获得此 error_code 的 error_category
    (公开成员函数)

    default_error_condition

    获得此 error_code 的 error_condition
    (公开成员函数)

    message

    获得此 error_code 的解释性字符串
    (公开成员函数)

    operator bool

    检查值是否非零
    (公开成员函数)


    下面是具体的示例:

    1. //error_code observers: value, category and message
    2. #include //std::cout, std::ios
    3. #include //std::system_error
    4. #include //std::ifstream
    5. #include //std::string
    6. using std::cout;
    7. using std::ios;
    8. using std::endl;
    9. using std::system_error;
    10. using std::ifstream;
    11. using std::string;
    12. int main()
    13. {
    14. std::error_code code;
    15. cout << "Code: " << code.value() << endl;
    16. cout << "Category: " << code.category().name() << endl;
    17. cout << "Message: " << code.message() << endl;
    18. cout << "default_error_condition: " << code.default_error_condition().message() << endl;
    19. cout << "bool: " << code.operator bool() << endl;
    20. cout << "Hello World!" << endl;
    21. return 0;
    22. }

    运行结果:

     上面是创建一个error_code 对象,程序正常运行,根据上面的结果可以看没程序正常没有错误。

    下面我们来看一个异常的程序。

    1. //error_code observers: value, category and message
    2. #include //std::cout, std::ios
    3. #include //std::system_error
    4. #include //std::ifstream
    5. #include //std::string
    6. using std::cout;
    7. using std::ios;
    8. using std::endl;
    9. using std::system_error;
    10. using std::ifstream;
    11. using std::string;
    12. int main()
    13. {
    14. ifstream is;
    15. is.exceptions(std::ios::failbit);
    16. try {
    17. is.open("unexistent.txt");
    18. } catch (const std::system_error &e) {
    19. cout << "Exception caught (system_error):\n" << endl;
    20. cout << "Error: " << e.what() << endl;
    21. cout << "Code: " << e.code().value() << endl;
    22. cout << "Category: " << e.code().category().name() << endl;
    23. cout << "Message: " << e.code().message() << endl;
    24. cout << "default_error_condition: " << e.code().default_error_condition().message() << endl;
    25. cout << "bool: " << e.code().operator bool() << endl;
    26. cout << endl;
    27. }
    28. cout << "Hello World!" << endl;
    29. return 0;
    30. }

    运行结果:

     打开一个不存在的文件,出现了异常,从上面的异常提示我们知道这是输入输出流错误。

    接着,我们把这个错误对象赋给第一个程序创建的一个空的错误类对象。查看他的信息。

    1. //error_code observers: value, category and message
    2. #include //std::cout, std::ios
    3. #include //std::system_error
    4. #include //std::ifstream
    5. #include //std::string
    6. using std::cout;
    7. using std::ios;
    8. using std::endl;
    9. using std::system_error;
    10. using std::ifstream;
    11. using std::string;
    12. int main()
    13. {
    14. std::error_code code;
    15. cout << "Code: " << code.value() << endl;
    16. cout << "Category: " << code.category().name() << endl;
    17. cout << "Message: " << code.message() << endl;
    18. cout << "default_error_condition: " << code.default_error_condition().message() << endl;
    19. cout << "bool: " << code.operator bool() << endl;
    20. cout << endl;
    21. ifstream is;
    22. is.exceptions(std::ios::failbit);
    23. try {
    24. is.open("unexistent.txt");
    25. } catch (const std::system_error &e) {
    26. cout << "Exception caught (system_error):\n" << endl;
    27. cout << "Error: " << e.what() << endl;
    28. cout << "Code: " << e.code().value() << endl;
    29. cout << "Category: " << e.code().category().name() << endl;
    30. cout << "Message: " << e.code().message() << endl;
    31. cout << "default_error_condition: " << e.code().default_error_condition().message() << endl;
    32. cout << "bool: " << e.code().operator bool() << endl;
    33. cout << endl;
    34. // operator= 操作
    35. code = e.code();
    36. cout << "Code: " << code.value() << endl;
    37. cout << "Category: " << code.category().name() << endl;
    38. cout << "Message: " << code.message() << endl;
    39. cout << "default_error_condition: " << code.default_error_condition().message() << endl;
    40. cout << "bool: " << code.operator bool() << endl;
    41. cout << endl;
    42. }
    43. cout << "Hello World!" << endl;
    44. return 0;
    45. }

    运行结果:

     e和code两个错误对象的信息相同,接下来再调用clear函数,把信息内容清空

    1. //error_code observers: value, category and message
    2. #include //std::cout, std::ios
    3. #include //std::system_error
    4. #include //std::ifstream
    5. #include //std::string
    6. using std::cout;
    7. using std::ios;
    8. using std::endl;
    9. using std::system_error;
    10. using std::ifstream;
    11. using std::string;
    12. int main()
    13. {
    14. std::error_code code;
    15. cout << "Code: " << code.value() << endl;
    16. cout << "Category: " << code.category().name() << endl;
    17. cout << "Message: " << code.message() << endl;
    18. cout << "default_error_condition: " << code.default_error_condition().message() << endl;
    19. cout << "bool: " << code.operator bool() << endl;
    20. cout << endl;
    21. ifstream is;
    22. is.exceptions(std::ios::failbit);
    23. try {
    24. is.open("unexistent.txt");
    25. } catch (const std::system_error &e) {
    26. cout << "Exception caught (system_error):\n" << endl;
    27. cout << "Error: " << e.what() << endl;
    28. cout << "Code: " << e.code().value() << endl;
    29. cout << "Category: " << e.code().category().name() << endl;
    30. cout << "Message: " << e.code().message() << endl;
    31. cout << "default_error_condition: " << e.code().default_error_condition().message() << endl;
    32. cout << "bool: " << e.code().operator bool() << endl;
    33. cout << endl;
    34. // operator= 操作
    35. code = e.code();
    36. cout << "Code: " << code.value() << endl;
    37. cout << "Category: " << code.category().name() << endl;
    38. cout << "Message: " << code.message() << endl;
    39. cout << "default_error_condition: " << code.default_error_condition().message() << endl;
    40. cout << "bool: " << code.operator bool() << endl;
    41. cout << endl;
    42. //调用clear()
    43. code.clear();
    44. cout << "Code: " << code.value() << endl;
    45. cout << "Category: " << code.category().name() << endl;
    46. cout << "Message: " << code.message() << endl;
    47. cout << "default_error_condition: " << code.default_error_condition().message() << endl;
    48. cout << "bool: " << code.operator bool() << endl;
    49. cout << endl;
    50. }
    51. cout << "Hello World!" << endl;
    52. return 0;
    53. }

    运行结果:

     code对象又恢复到初始状态了。

    我们也可以对错误码对像进行hash:

    1. //error_code observers: value, category and message
    2. #include //std::cout, std::ios
    3. #include //std::system_error
    4. #include //std::ifstream
    5. #include //std::string
    6. using std::cout;
    7. using std::ios;
    8. using std::endl;
    9. using std::system_error;
    10. using std::ifstream;
    11. using std::string;
    12. int main()
    13. {
    14. ifstream is;
    15. is.exceptions(std::ios::failbit);
    16. try {
    17. is.open("unexistent.txt");
    18. } catch (const std::system_error &e) {
    19. cout << "Exception caught (system_error):\n" << endl;
    20. cout << "Error: " << e.what() << endl;
    21. cout << "Code: " << e.code().value() << endl;
    22. cout << "Category: " << e.code().category().name() << endl;
    23. cout << "Message: " << e.code().message() << endl;
    24. cout << "default_error_condition: " << e.code().default_error_condition().message() << endl;
    25. cout << "bool: " << e.code().operator bool() << endl;
    26. cout << endl;
    27. std::hash hashErrorCode;
    28. cout << "hash(error_code): " << hashErrorCode(e.code()) << endl;
    29. }
    30. cout << "Hello World!" << endl;
    31. return 0;
    32. }

    运行结果:

     hash这是一个结构体模板,它不仅可以对错误码对象进行hash也可以对数值进行hash,比如int, long long, float, double,string等,下面来看一个示例:

    1. //error_code observers: value, category and message
    2. #include //std::cout, std::ios
    3. #include //std::system_error
    4. #include //std::ifstream
    5. #include //std::string
    6. using std::cout;
    7. using std::ios;
    8. using std::endl;
    9. using std::system_error;
    10. using std::ifstream;
    11. using std::string;
    12. int main()
    13. {
    14. ifstream is;
    15. is.exceptions(std::ios::failbit);
    16. try {
    17. is.open("unexistent.txt");
    18. } catch (const std::system_error &e) {
    19. cout << "Exception caught (system_error):\n" << endl;
    20. cout << "Error: " << e.what() << endl;
    21. cout << "Code: " << e.code().value() << endl;
    22. cout << "Category: " << e.code().category().name() << endl;
    23. cout << "Message: " << e.code().message() << endl;
    24. cout << "default_error_condition: " << e.code().default_error_condition().message() << endl;
    25. cout << "bool: " << e.code().operator bool() << endl;
    26. cout << endl;
    27. std::hash hashErrorCode;
    28. cout << "hash(error_code): " << hashErrorCode(e.code()) << endl;
    29. string str = "hello world!";
    30. std::hash hashStr;
    31. cout << "hash(string): " << hashStr(str) << endl;
    32. int n = 100;
    33. std::hash<int> hashN;
    34. cout << "hash(int): " << hashN(n) << endl;
    35. long long ln = 100;
    36. std::hash<long long> hashLn;
    37. cout << "hash(long long): " << hashLn(ln) << endl;
    38. double dou = 100;
    39. std::hash<double> hashD;
    40. cout << "hash(double): " << hashD(dou) << endl;
    41. float f = 100;
    42. std::hash<float> hashF;
    43. cout << "hash(float): " << hashF(f) << endl;
    44. }
    45. cout << "Hello World!" << endl;
    46. return 0;
    47. }

     运行结果:

    参考:

    - C++ Reference

    标准库头文件 - cppreference.com

  • 相关阅读:
    HRNet-Facial-Landmark-Detection 训练自己数据集
    自学CFD:我在实习岗速成无人机设计和仿真的故事
    基于粒子群优化算法的微型燃气轮机冷热电联供系统优化调度(Matlab代码实现)
    wireshark常见使用表达式
    redis的原理和源码-sentinel哨兵的原理和源码解析(上)
    RAID卡管理工具使用
    冒泡排序(学习笔记)
    Ubuntu服务器安装Nvidia显卡驱动各类失败问题的解决方案集合
    开源3D激光(视觉)SLAM算法汇总(持续更新)
    Java小树的参天成长(构造方法重载)
  • 原文地址:https://blog.csdn.net/chenyijun/article/details/125822455