When i am inheriting from std::exception
in order to define my own exception type, i need to override the what()
method, which has the following signature:
virtual const char* what() const throw();
This definitely looks strange to me, like if there were two method names in the signature. Is this some very specific syntax, like with pure virtual methods, e.g.:
virtual int开发者_运维技巧 method() const = 0;
or is this a feature, that could somehow be used in another context, too? And if so, for what could it be used?
It is called exception specifications
. The throw()
doesn't allow any exception to be thrown from inside this method throw(int)
would only allow exceptions of type int
to be thrown.
Exception specifications will be dropped in C++0x. This gives a very good explanation of the reasons.
virtual const char* what() const throw();
what()
is a virtual constant method that returns pointer to const char
and must not throw any exception. Not very strange method declaration :)
精彩评论