• c++异常详解


    目录

    1. c语言传统处理错误的方法

    2. c++异常的概念

    3.异常的使用

    4.c++标准库的异常体系

    5.异常的优缺点


    1. c语言传统处理错误的方法

    (1) 终止程序: assert, 非常之暴力,直接终止进程, 不管是否轻重的异常.

    (2) 返回错误码: 需要对错误码进行查找.

     2. c++异常的概念

    (1) 当函数无法处理的错误的时候就可以抛异常, 让函数直接/间接调用的操作者进行修改错误.

    (2) 一般会采用到三个关键字来处理: 看代码更好理解

            throw:

                    当程序异常的时候, 就会抛出异常.

            catch:

                    用于进行异常捕获.

            try:

                    被激活异常的条件.

     

    3.异常的使用

    3.1 异常的捕获和抛出:

    (1) 异常的抛出和匹配原则:

            被选中处理代码是与最近的那一个进行匹配的.

            抛出异常之后, 会产生一个异常对象的拷贝, 在catch之后被销毁.

    (2) 采用的原则:

            先检查throw是否在try里面, 再匹配合适的catch;

            如果没用合适的catch就会退出当前函数栈,如果到达main函数的栈,依旧没有匹配的,则终止程序。 

    1. class A
    2. {
    3. public:
    4. A()
    5. {
    6. cout << "A()" << endl;
    7. }
    8. ~A()
    9. {
    10. cout << "~A()" << endl;
    11. }
    12. };
    13. double Division(int len, int time)
    14. {
    15. if (time == 0)
    16. {
    17. throw "除0错误";
    18. }
    19. else
    20. {
    21. return (double)len / (double)time;
    22. }
    23. }
    24. void Func()
    25. {
    26. A aa;
    27. try
    28. {
    29. int len, time;
    30. cin >> len >> time;
    31. cout << Division(len, time) << endl;
    32. }
    33. catch(const char* s)
    34. {
    35. cout << s << endl;
    36. }
    37. cout << ">>>>>>>>>>>>>>" << endl;
    38. }
    39. int main()
    40. {
    41. try
    42. {
    43. Func();
    44. }
    45. catch (const char* str)
    46. {
    47. cout << str << endl;
    48. }
    49. return 0;
    50. }

     3.2 异常的重新抛出

            处理一些可能当个catch无法完成的异常, 通过重新抛异常交给上层函数进行处理.

    1. double Division(int len, int time)
    2. {
    3. if (time == 0)
    4. {
    5. throw "除0错误";
    6. }
    7. else
    8. {
    9. return (double)len / (double)time;
    10. }
    11. }
    12. void Func()
    13. {
    14. int* arry = new int[10];
    15. try
    16. {
    17. int len, time;
    18. cin >> len >> time;
    19. cout << Division(len, time) << endl;
    20. }
    21. catch (...) //... 表示的是到这里说明有人没有按规范(约定)抛异常
    22. {
    23. cout << "delete []" << arry << endl;
    24. delete[] arry;
    25. throw;
    26. }
    27. }
    28. int main()
    29. {
    30. try
    31. {
    32. Func();
    33. }
    34. catch (const char* errmsg)
    35. {
    36. cout << errmsg << endl;
    37. }
    38. return 0;
    39. }

    3.3 异常安全

    最好不要再构造函数和析构函数里面调用异常函数;  因为构造函数在完成对象的构造和初始化的时候被中断, 造成对象不完整或者没有初始化.

    析构函数也是在完成对对象内存的销毁不完整和没有销毁导致内存泄漏的危险.

    3.4 异常规范

    (1) 如果要抛出某种类型的异常; 函数()后面 + throw(类型);

    (2) 如果不抛出异常; 函数()后面 + throw();

    (3) 关键字noexcept, 加在函数后面就是不需要抛异常.

     3.5 自定义异常体系

    1. class Execption
    2. {
    3. public:
    4. Execption(const string& errmsg, int id)
    5. :_errmsg(errmsg)
    6. ,_id(id)
    7. {}
    8. virtual string what()const
    9. {
    10. return _errmsg;
    11. }
    12. protected:
    13. string _errmsg;
    14. int _id;
    15. };
    16. class SqlExecption : public Execption
    17. {
    18. public:
    19. SqlExecption(const string& errmsg, int id, const string& sql)
    20. :Execption(errmsg, id)
    21. ,_sql(sql)
    22. {}
    23. virtual string what()const
    24. {
    25. string str = "SqlExecption:";
    26. str += _errmsg;
    27. str += "->";
    28. str += _sql;
    29. return _sql;
    30. }
    31. private:
    32. const string _sql;
    33. };
    34. class CacheException : public Execption
    35. {
    36. public:
    37. CacheException(const string& errmsg, int id)
    38. :Execption(errmsg, id)
    39. {}
    40. virtual string what() const
    41. {
    42. string str = "CacheException:";
    43. str += _errmsg;
    44. return str;
    45. }
    46. };
    47. class HttpServerException : public Execption
    48. {
    49. public:
    50. HttpServerException(const string& errmsg, int id, const string& type)
    51. :Execption(errmsg, id)
    52. , _type(type)
    53. {}
    54. virtual string what() const
    55. {
    56. string str = "HttpServerException:";
    57. str += _type;
    58. str += ":";
    59. str += _errmsg;
    60. return str;
    61. }
    62. private:
    63. const string _type;
    64. };
    65. void SQLMgr()
    66. {
    67. srand(time(0));
    68. if (rand() % 7 == 0)
    69. {
    70. throw SqlExecption("权限不足", 100, "select * from name = '张三'");
    71. }
    72. }
    73. void cacheMgr()
    74. {
    75. srand(time(0));
    76. if (rand() % 5 == 0)
    77. {
    78. throw CacheException("权限不足", 100);
    79. }
    80. else if (rand() % 6 == 0)
    81. {
    82. throw CacheException("数据不存在", 101);
    83. }
    84. SQLMgr();
    85. }
    86. void HttpServer()
    87. {
    88. // ...
    89. srand(time(0));
    90. if (rand() % 3 == 0)
    91. {
    92. throw HttpServerException("请求资源不存在", 100, "get");
    93. }
    94. else if (rand() % 4 == 0)
    95. {
    96. throw HttpServerException("权限不足", 101, "post");
    97. }
    98. cacheMgr();
    99. }

    4.c++标准库的异常体系

    这里用一下其他博主的图. 实际中是继承了exception的类来实现自己的异常类

     

    1. int main()
    2. {
    3. try
    4. {
    5. vector<int> v(10, 5);
    6. //v.resize(100000000);
    7. v.at(10) = 100;
    8. }
    9. catch(const exception& e)
    10. {
    11. cout << e.what() << endl;
    12. }
    13. catch (...)
    14. {
    15. cout << "Unkown Exception" << endl;
    16. }
    17. return 0;
    18. }

     异常为invalid vector subscript(无效的矢量下标).

     5.异常的优缺点

    5.1 优点:

    (1) 相比于错误码更加清晰的可以看出来异常的信息, 更好的找到bug.

    (2) 返回错误码的操作需要跳到最外层才可以拿到错误信息, catch直接跳出函数到main函数处理错误.

    (3) 很多第三方库都是使用到异常.

    (4) 部分函数适合使用异常, 比如有些函数是没有返回值; 例如构造函数, 还有越界处理.

     5.2 缺点:

    (1) 异常会导致程序执行乱跳, 非常混乱. 就会难以调试.

    (2) c++没有垃圾回收机制, 如果有些资源在catch之前没有回收就会导致资源泄漏. 可以使用RAII来处理资源的管理问题

    (3) 使用异常的使用最好要规范:

            首先就是抛出异常类型都是继承来一个类; 其次就是函数抛异常, 抛声明异常最好写清楚.

  • 相关阅读:
    【单片机项目实训】八路抢答器
    定义和声明
    LVS负载均衡群集--NAT
    利用servlet实现对书籍书名、单价、数量等信息的添加,计算总价
    Nvidia Jetson Nano学习笔记--使用C语言实现GPIO控制
    kafka消费/发送消息,消息过大报错解决whose size is larger than the fetch size 1048576
    UE4和C++ 开发-C++绑定widget的方式和初始化UI
    【对比学习】CUT模型论文解读与NCE loss代码解析
    学习SLAM:SLAM进阶(九)以激光点云赋色为例讲述如何自定义ROS的消息格式并实现消息的订阅与发布
    基于线性核函数的SVM数据分类算法matlab仿真
  • 原文地址:https://blog.csdn.net/huajiahhhh/article/details/138167042