'Virtual const char* ro_err::StdErr::what() const' 的更宽松的抛掷

looser throw specifier for ‘virtual const char* ro_err::StdErr::what() const’

本文关键字:const what ro err StdErr Virtual char      更新时间:2023-10-16

这是我的完整代码,我自定义异常如下:

class StdErr : public std::exception {
public:
    str msg;
    StdErr(str msg) { this->msg = msg; };
    virtual const char *what() const override {
        return this->msg.c_str();
    };
};

并像这样继承它:

class ShErr : public StdErr {
public:
    ShErr(str m) : StdErr(m) { }
};

并像这样使用它们:

int main(int argc, char **argv) {
    throw ro_err::ShErr("sh err");
    return (0);
};

它提出了looser throw specifier for ‘virtual const char* ro_err::StdErr::what() const’,我有以下问题:

  • 什么是更宽松的?
  • 什么是说明符?
  • 如何修复它

由于 c++11 what() 是没有例外的。 你没有宣布它为noexcept。 这就是"更宽松的投掷说明符"告诉你的。请参阅 http://en.cppreference.com/w/cpp/error/exception/what。

即声明它像

virtual const char *what() const noexcept override