Here is from http://www.tutorialspoint.com/cplusplus/cpp_exceptions_handling.htm
#include <iostream>
#include <exception>
using namespace std;
struct MyException : public exception
{
const char * what () const throw ()
{
return "C++ Exception";
}
};
I understand the const
after what
means the function does not modify any
members of the struct, but what does the throw()
at the end mean?
See here: http://www.cplusplus.com/doc/tutorial/exceptions/
It means it won't throw any exceptions. This is an important guarantee for a function like
what
, which is usually called in exception handling: you don't want another exception to be thrown while you're trying to handle one.In C++11, you generally should use
noexcept
instead. The old throw specification is deprecated.throw ()
is an exception specifier that declares thatwhat()
will never throw an exception. This is deprecated in C++11, however (see http://en.wikipedia.org/wiki/C++11). To specify that a function does not throw any exception, thenoexcept
keyword exists in C++11.