一、异常处理的思想与程序实现

异常处理的基本思想

1236682439804456960.png

异常处理的语法

1236682499128692736.png

注意这里的cath异常类型是不支持隐式转化的

例12-1处理除零异常

  1. //12_1.cpp
  2. #include <iostream>
  3. using namespace std;
  4. int divide(int x, int y) {
  5. if (y == 0)
  6. throw x;
  7. return x / y;
  8. }
  9. int main() {
  10. try {
  11. cout << "5 / 2 = " << divide(5, 2) << endl;
  12. cout << "8 / 0 = " << divide(8, 0) << endl;
  13. cout << "7 / 1 = " << divide(7, 1) << endl;
  14. } catch (int e) {
  15. cout << e << " is divided by zero!" << endl;
  16. }
  17. cout << "That is ok." << endl;
  18. return 0;
  19. }

异常接口声明

  • 一个函数显式声明可能抛出的异常,有利于函数的调用者为异常处理做好准备
  • 可以在函数的声明中列出这个函数可能抛掷的所有异常类型void fun() throw(A,B,C,D);

  • 若无异常接口声明,则此函数可以抛掷任何类型的异常。

  • 不抛掷任何类型异常的函数声明如下:void fun() throw();

二、异常处理中的构造与析构

自动的析构

  • 找到一个匹配的catch异常处理后
    • 初始化异常参数。
    • 将从对应的try块开始到异常被抛掷处之间构造(且尚未析构)的所有自动对象进行析构。
  • 从最后一个catch处理之后开始恢复执行。

例12-2 带析构语义的类的C++异常处理

  1. //12_2.cpp
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. class MyException {
  6. public:
  7. MyException(const string &message) : message(message) {}
  8. ~MyException() {}
  9. const string &getMessage() const { return message; }
  10. private:
  11. string message;
  12. };
  13. class Demo {
  14. public:
  15. Demo() { cout << "Constructor of Demo" << endl; }
  16. ~Demo() { cout << "Destructor of Demo" << endl; }
  17. };
  18. void func() throw (MyException) {
  19. Demo d;
  20. cout << "Throw MyException in func()" << endl;
  21. throw MyException("exception thrown by func()");
  22. }
  23. int main() {
  24. cout << "In main function" << endl;
  25. try {
  26. func();
  27. } catch (MyException& e) {
  28. cout << "Caught an exception: " << e.getMessage() << endl;
  29. }
  30. cout << "Resume the execution of main()" << endl;
  31. return 0;
  32. }
  33. 运行结果:
  34. In main function
  35. Constructor of Demo
  36. Throw MyException in func()
  37. Destructor of Demo
  38. Caught an exception: exception thrown by func()
  39. Resume the execution of main()

三、标准库异常处理

标准异常类的继承关系

1236683751526895616.png

C++标准库各种异常类所代表的异常

1236683802936479744.png

标准异常类的基础

  • exception:标准程序库异常类的公共基类
  • logic_error表示可以在程序中被预先检测到的异常
    • 如果小心地编写程序,这类异常能够避免
  • runtime_error表示难以被预先检测的异常

例12-3 三角形面积计算

  • 编写一个计算三角形面积的函数,函数的参数为三角形三边边长a、b、c,可以用Heron公式计算:```cpp //12_3.cpp

    include

    include

    include

    using namespace std; //给出三角形三边长,计算三角形面积 double area(double a, double b, double c) throw (invalid_argument) { //判断三角形边长是否为正 if (a <= 0 || b <= 0 || c <= 0)
    1. throw invalid_argument("the side length should be positive");
    //判断三边长是否满足三角不等式 if (a + b <= c || b + c <= a || c + a <= b)
    1. throw invalid_argument("the side length should fit the triangle inequation");
    //由Heron公式计算三角形面积 double s = (a + b + c) / 2; return sqrt(s (s - a) (s - b) * (s - c)); } int main() { double a, b, c; //三角形三边长 cout << “Please input the side lengths of a triangle: “; cin >> a >> b >> c; try {
    1. double s = area(a, b, c); //尝试计算三角形面积
    2. cout << "Area: " << s << endl;
    } catch (exception &e) {
    1. cout << "Error: " << e.what() << endl;
    } return 0; } • 运行结果1: Please input the side lengths of a triangle: 3 4 5 Area: 6 • 运行结果2: Please input the side lengths of a triangle: 0 5 5 Error: the side length should be positive • 运行结果2: Please input the side lengths of a triangle: 1 2 4 Error: the side length should fit the triangle inequation ```
  1. #include <iostream>
  2. using namespace std;
  3. struct CException
  4. {void reason() const{ cout<< "a expected exception accured"<<endl;}};
  5. void func(int n) {
  6. throw(CException());
  7. cout<<n<<endl;
  8. }
  9. int main(){
  10. try
  11. {func(666);}
  12. catch(const CException& e)
  13. { e.reason();}
  14. return 0;
  15. }