C++20 sections · 1024 units
Open in Course

The what Method

Getting error descriptions

The what() method returns a C-style string (const char*) describing the exception. Every standard exception has it. When you construct an exception with a message, what() returns that message: throw runtime_error("File not found"); Catching it: e.what() returns "File not found".

For custom exceptions, override what(): const char* what() const noexcept override { return message.c_str(); } The noexcept is required because the base class declares it. Good error messages include context: not just "Error" but "Failed to open config.txt: permission denied".

This makes debugging much easier.