Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

c++11 - Looser Throw Specifier in C++

What does this error mean? How can I fix it? This is the header code that's causing it:

class BadJumbleException : public exception {
public:
    BadJumbleException (const string& msg); // Constructor, accepts a string as the message
    string& what();                         // Returns the message string
private:
    string message;                         // Stores the exception message
};

And this is the source code:

BadJumbleException::BadJumbleException (const string& m) : message(m) {}
string& BadJumbleException::what() { return message; }

EDIT: This is the error:

looser throw specifier for 'virtual BadJumbleException::~BadJumbleException()

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In C++03, per §18.6.1/5, std::exception has a destructor that is declared such that no exceptions can be thrown out of it (a compilation error will be caused instead).

The language requires that when you derive from such a type, your own destructor must have the same restriction:

virtual BadJumbleException::~BadJumbleException() throw() {}
//                                                ^^^^^^^

This is because an overriding function may not have a looser throw specification.


In C++11, std::exception::~exception is not marked throw() (or noexcept) explicitly in the library code, but all destructors are noexcept(true) by default.

Since that rule would include your destructor and allow your program to compile, this leads me to conclude that you are not really compiling as C++11.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...