简体   繁体   中英

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

here is my full code , I customize exception like:

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();
    };
};

and inherite it like:

class ShErr : public StdErr {

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

and use them like:

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

it raise looser throw specifier for 'virtual const char* ro_err::StdErr::what() const' , I have following questions:

  • what is looser?
  • what is specifier?
  • how to fix it

Since c++11 what() is noexcept . You have not declared it as noexcept . That is what the 'looser throw specifier' is telling you. See http://en.cppreference.com/w/cpp/error/exception/what .

Ie declare it like

virtual const char *what() const noexcept override

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM