这是我的 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++11 what()
是 noexcept。您尚未将其声明为 noexcept。这就是“宽松 throw 说明符”告诉您的内容。参见 http://en.cppreference.com/w/cpp/error/exception/what .
即像这样声明
virtual const char *what() const noexcept override
关于c++ - ‘virtual const char* ro_err::StdErr::what() const’ 的宽松 throw 说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35763027/