C++ stdexcept what() Function
Example
Get the message of an exception:
try {
throw runtime_error("Could not save the file");
} catch (const exception& e) {
cout << e.what();
}
Definition and Usage
The what() function returns the message of an exception, as a C-style string (const char*).
For the classes in <stdexcept>, it returns the exact text that you gave when you threw the exception.
The what function is defined in the <exception> header file.
Note: Some library functions throw exceptions too, like at() of a vector. The text that what() returns for them is different for each compiler, so do not depend on it.
Syntax
exception.what()
exception is the caught exception object, like e in catch (const exception& e).
Parameter Values
None.
Technical Details
| Returns: | The message of the exception, as a C-style string (const char*). |
|---|
Related Pages
Read more about exceptions in our Exceptions Tutorial.