12_异常处理

异常处理

异常处理的思想

12-1.png

12-2.png

#include <iostream>
using namespace std;
int divide(int x, int y) {
    if (y == 0)
        throw x;
    return x / y;
}
int main() {
    try {
        cout << "5 / 2 = " << divide(5, 2) << endl;
        cout << "8 / 0 = " << divide(8, 0) << endl;
        cout << "7 / 1 = " << divide(7, 1) << endl; //未被执行
    } catch (int e) {
        cout << e << " is divided by zero!" << endl;
    }
    cout << "That is ok." << endl;
    return 0;
}
//try语句块在抛出异常后,之后的语句变不在执行了
输出
2.5
8 is divided by zero!
That is ok.

异常的析构

try在捕获到异常后,会先抛出异常,然后将try语句开始到异常语句中定义的对象先进行自动的析构,然后再去catch中处理异常,不用担心内存泄漏问题。

#include <iostream>
#include <string>
using namespace std;
class MyException {
public:
    MyException(const string &message) : message(message) {}
    ~MyException() {}
    const string &getMessage() const { return message; }
private:
    string message;
};

class Demo {
public:
    Demo() { cout << "Constructor of Demo" << endl; }
    ~Demo() { cout << "Destructor of Demo" << endl; }
};
void func() throw (MyException) {
    Demo d;
    cout << "Throw MyException in func()" << endl;
    throw MyException("exception thrown by func()");
}

int main() {
    cout << "In main function" << endl;
    try {
        func();
    } catch (MyException& e) {
        cout << "Caught an exception: " << e.getMessage() << endl;
    } 
    cout << "Resume the execution of main()" << endl;
    return 0;
}
运行结果:
In main function
Constructor of Demo
Throw MyException in func()
Destructor of Demo
Caught an exception: exception thrown by func()
Resume the execution of main()

标准库异常处理

标准异常类的继承关系

12-3.png

12-4.png

标准异常类的基础

  • exception:标准程序库异常类的公共基类

  • logic_error表示可以在程序中被预先检测到的异常

    • 如果小心地编写程序,这类异常能够避免
  • runtime_error表示难以被预先检测的异常

#include <iostream>
#include <cmath>
#include <stdexcept>
using namespace std;
//给出三角形三边长,计算三角形面积
double area(double a, double b, double c)  throw (invalid_argument)
{
   //判断三角形边长是否为正
    if (a <= 0 || b <= 0 || c <= 0)
        throw invalid_argument("the side length should be positive");
   //判断三边长是否满足三角不等式
    if (a + b <= c || b + c <= a || c + a <= b)
        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 {
        double s = area(a, b, c);   //尝试计算三角形面积
        cout << "Area: " << s << endl;
    } catch (exception &e) {
        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
posted @ 2020-07-26 15:57  happy_fan  阅读(150)  评论(0编辑  收藏  举报