The following code works just fine:
#include <exception>
using namespace std;
class FileException : public exception { // error occurs here
int _error;
// string _error; <-- this would cause the error
public:
FileException(int error);
// FileException(string error);
const char* what() const throw();
};
But as soon as I change the type of _error to string, the following compile error occurs:
Exception specification of overriding function is more lax than base version
The destructor of
std::stringis not no-throw, which causes the implicit destructor ofFileExceptionnot no-throw either. But the destructor ofstd::exceptionis no-throw, thus there’s a compiler error.You could declare an explicit no-throw destructor:
or just inherit from
std::runtime_errorinstead ofstd::exception, which has a constructor that takesstd::stringinput.