Custom Exception Types
Define your own exceptions.
Why Custom Exceptions?
Defining your own exception types lets callers catch your errors specifically and carry domain-specific data with the error.
Derive from std::exception
Base your exceptions on std::exception (or a subclass) so they integrate with standard handlers and expose what().
#include <exception>
#include <string>
class ConfigError : public std::exception {
std::string msg;
public:
explicit ConfigError(std::string m) : msg(std::move(m)) {}
const char* what() const noexcept override { return msg.c_str(); }
};All lessons in this course
- try, catch, throw
- Exception Safety
- noexcept Specifier
- Custom Exception Types