Doing a project, I'm trying to create a HandleError header with a bunch of classes. Inside of my class BadNumber
, I have a public method/function that takes in as a string type
and num
. However, when I'm trying to test it, I get an error asking for exception specification of overriding function is more lax than base version
because I'm inheriting it from public std::exception
. I Googled around how to fix this and it is recommending me to do a noexcept override call in my what()
for this exception call (link here). The example is almost identical to mine with the same error message.
Code (compiling with GCC/Clang c++11):
#include <exception>
#include <string>
class BadNumber : public std::exception
{
private:
std::string _msg;
public:
BadNumber(std::string type, std::string num) : _msg(num + "is invalid type for " + type) {}
const char *what() const noexcept override
{
return (_msg.c_str());
}
};
Error message:
src/../inc/HandleError.hpp:23:15: error: exception specification of overriding function is more lax than base version
const char *what() const noexcept override
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/exception:102:25: note: overridden virtual function is here
virtual const char* what() const _NOEXCEPT;
I did what it was asked and did some research, but still no luck and still doing more. Your help is much appreciated and it will be always helpful to get some feedback and constructive as possible. Thank you for your time and patience :)