Looser throw specifier error in C++
looser throw specifier for 'virtual Error::~Error()' ... overriding 'virtual std::exception::~exception() throw ()'
While compiling some code with gcc (that had previously compiled and run without complaint under CodeWarrior), the following fatal error was reported:
Error.h:83: looser throw specifier for `virtual Error::~Error()' /usr/include/gcc/darwin/3.1/g++-v3/exception:54: overriding `virtual std::exception::~exception() throw ()'
The error message is completely correct, but deserves some explanation. It occurs when a method in a derived class has a different throw specifier (the set of allowed objects that can be thrown from within the method) to the base class. In the above example, the base class 'std::exception' is defined:
class exception { ... virtual ~exception() throw(); ... };
while the derived class 'Error' is defined:
class Error: public std::exception { ... ~Error (); ... };
Note that if no destructor is defined in the derived class, the problem still occurs as the automatically generated destructor is like that above, i.e. has no throw specifier. The appearance of this problem can be unpredictable as not all compilers will worry about this problem. (The above example was detected by gcc 3, but passed by gcc 2.9 and CodeWarrior 8.) Also, in the case of 'std::exception', not all implementions of the STL use throw specifiers. In any event, the derived class must be corrected:
class Error: public std::exception { ... ~Error () throw (); ... };