Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looser Throw Specifier in C++

Tags:

c++

c++11

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

like image 575
user2824889 Avatar asked Mar 27 '14 20:03

user2824889


1 Answers

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.

like image 142
Lightness Races in Orbit Avatar answered Nov 11 '22 22:11

Lightness Races in Orbit