0Pricing
C++ Academy · Lesson

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

  1. try, catch, throw
  2. Exception Safety
  3. noexcept Specifier
  4. Custom Exception Types
← Back to C++ Academy