【问题标题】:looser throw specifier for ‘virtual const char* ro_err::StdErr::what() const’'virtual const char* ro_err::StdErr::what() const' 的更宽松的抛出说明符
【发布时间】:2016-03-03 04:10:35
【问题描述】:

这是我的full code,我自定义异常如下:

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++


    【解决方案1】:

    由于 c++11 what()noexcept。您尚未将其声明为 noexcept。这就是“宽松的抛出说明符”告诉你的。见http://en.cppreference.com/w/cpp/error/exception/what

    即像这样声明它

    virtual const char *what() const noexcept override
    

    【讨论】:

    • 宽松是什么意思?
    • @asullaherc 这是对 throw 规范的不太严格的定义。没有它 what() 允许抛出异常。
    • virtual 不需要,除非你仍然想覆盖这个方法
    • @PawełIwaneczko 在继承类中省略 virtual 对代码的行为完全没有影响。 IE。它仍然可以被覆盖。
    猜你喜欢
    • 2011-02-10
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    相关资源
    最近更新 更多