C++ Academy · Lesson

Custom Exception Types

Define your own exceptions.

Lesson 4 of 413 steps

Custom Exception Types is a free C++ Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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(); }
};

Throwing and Catching It

Your type behaves like any other exception: throw it, catch it by reference.

#include <iostream>
#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(); }
};

int main() {
    try {
        throw ConfigError("missing key");
    } catch (const ConfigError& e) {
        std::cout << e.what() << "\n";
    }
}

Reuse Standard Bases

Often you can derive from std::runtime_error or std::logic_error, which already store a message — less boilerplate.

#include <stdexcept>
#include <string>

class NetworkError : public std::runtime_error {
public:
    explicit NetworkError(const std::string& m) : std::runtime_error(m) {}
};

Carrying Extra Data

Custom exceptions can hold fields like an error code or the offending value, giving handlers more context.

#include <stdexcept>
#include <string>

class HttpError : public std::runtime_error {
public:
    int status;
    HttpError(int s, const std::string& m)
        : std::runtime_error(m), status(s) {}
};

Catching by Base Class

Because your type derives from std::exception, a generic handler still catches it via the base reference.

#include <iostream>
#include <stdexcept>

class HttpError : public std::runtime_error {
public:
    int status;
    HttpError(int s, const std::string& m) : std::runtime_error(m), status(s) {}
};

int main() {
    try {
        throw HttpError(404, "not found");
    } catch (const std::exception& e) {
        std::cout << e.what() << "\n";
    }
}

Build an Exception Hierarchy

Group related errors under a common base so callers can catch a whole family or a single specific type.

#include <stdexcept>

struct AppError : std::runtime_error { using std::runtime_error::runtime_error; };
struct DbError  : AppError       { using AppError::AppError; };
struct AuthError: AppError       { using AppError::AppError; };

Keep what() noexcept

Override what() as const noexcept and return a stable buffer — it must never throw or return a dangling pointer.

Throw by Value

Throw exception objects by value (e.g. throw HttpError(500, "x");). The runtime manages their storage during unwinding.

Document What You Throw

Make your API's exception types part of its contract so callers know which catches to write.

Keep It Simple

Most projects need only a small handful of exception types. Deriving from std::runtime_error covers the majority of cases cleanly.

Quick Check

Test your understanding of custom exceptions.

Recap

You learned to define custom exception types by deriving from std::exception or std::runtime_error, carry extra data, build hierarchies, keep what() noexcept, and throw by value. Keep the set small and documented.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Custom Exception Types” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Custom Exception Types”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “Custom Exception Types”?

Define your own exceptions. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Custom Exception Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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