public member function
<exception>
virtual const char* what() const throw();
virtual const char* what() const noexcept;
Get string identifying exception
Returns a null terminated character sequence that may be used to identify the exception.
The particular representation pointed by the returned value is implementation-defined.
As a virtual function, derived classes may redefine this function so that specific values are returned.
Return Value
A pointer to a c-string with content related to the exception.
This is guaranteed to be valid at least until the exception object from which it is obtained is destroyed or until a non-const member function of the exception object is called.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
// exception::what
#include <iostream> // std::cout
#include <exception> // std::exception
struct ooops : std::exception {
const char* what() const noexcept {return "Ooops!\n";}
};
int main () {
try {
throw ooops();
} catch (std::exception& ex) {
std::cout << ex.what();
}
return 0;
}
|
Possible output:
Exception safety
No-throw guarantee: this member function never throws exceptions.
This also applies to all derived classes within the C++ standard library.