C++自定义异常类报错

因为异常这个知识点好久没用,有点忘记了(我初学),打算给之前写的东西弄个自定义异常,练习一下就当复习了

class Treeexception : public exception {
    string error_str;
public:
    Treeexception(const char* ex){
        error_str = ex;
    }
    Treeexception(string ex){
        error_str = ex;
    }
    const char* what() const throw(){
        return this->error_str.c_str();
    }
    virtual ~Treeexception(){}
};

打算测试一下


    void func() throw(Treeexception) {
        throw Treeexception("test !");
    }

然后 报错 :

img

C++11不赞成使用Dynamic Exception Specification,C++17已经不支持Dynamic Exception Specification
https://en.cppreference.com/w/cpp/language/except_spec