Reputation: 3895
The following is a custom exception class from another person's c++ tutorial:
class MyException : public std::exception {
public:
MyException(const std::string message) : m_message(message) { }
~MyException() throw() { }
const std::string what() const throw() { return m_message.c_str(); }
private:
std::string m_message;
}
I get most of what going on here,, except for the "throw()" right next to the destructor..
Does it mean throw() is called whenever the exception class is destructed..??
Upvotes: 0
Views: 475
Reputation: 54589
This is an exception specification.
It means that the destructor is not supposed to throw any exception. If it attempts to throw one anyway, the program will invoke std::terminate
(which will almost certainly crash the program). Note that not all compilers implement this behavior correctly (most notably, in VC++ throwing from a destructor declared throw()
leads to unspecified behavior).
Note that exception specifications have been deprecated with C++11 in favor of noexcept and should no longer be used (for good reasons). Destructors are implicitly noexcept in C++11.
Upvotes: 5
Reputation: 308158
It's a declaration of what the function is allowed to throw. In this case, nothing.
See http://en.cppreference.com/w/cpp/language/except_spec
If you don't list an exception type inside the throw()
and you later try to throw one from that function, you'll get an std::unexpected
thrown instead.
If the throw()
is left out as it usually is, then any exception may be thrown.
Upvotes: 2