I am coming from a Ruby and Java background and have recently begun exploring C++.
While my initial attempts at creating custom exceptions by simply subclassing exception class failed with obscure, I found the following example posted on a site:
class Exception : public exception
{
public:
Exception(string m="exception!") : msg(m) {}
~Exception() throw() {}
const char* what() const throw() { return msg.c_str(); }
private:
string msg;
};
My understanding of semantics of C++ is not very mature at the moment, and I would like to have a better understanding of what is going on here.
In the statement const char* what() const throw()
what does the part const throw()
do, and what kind of programming construct is it?
Also, what is the purpose and intent of throw()
in the destructor specification ~Exception()
and why do I need to have a destructor specification although I don't need it do something in particular? Shouldn't the destructor
inherited from exception be sufficient?
const
andthrow()
are two different, unrelated things. – Necessitystd::runtime_error
rather thanstd::exception
. Then you don't need to have your membermsg
or definewhat()
or a destructor.class Exception : public std::runtime_error { public: Exception(string m="exception!") :std::runtime_error(m) {}};
– Play