Skip to content

Exceptions and Error Handling

Beginning of the lesson

Exceptions and Error Handling

Report failures cleanly, recover safely, and keep your objects consistent

Big Idea: An exception is a way to signal that a function could not do its job and to hand the problem to code that is prepared to deal with it. Instead of returning a magic value that a caller might ignore, you throw an error object; the language then unwinds the stack until a matching catch handles it. Done well, this keeps the "happy path" of your code clean and guarantees that resources are released even when things go wrong.
By the end of this topic you will be able to:
  • use throw, try and catch correctly, and throw objects derived from std::exception,
  • explain stack unwinding and why RAII is what makes exceptions safe,
  • reason about the three exception-safety guarantees (basic, strong, nothrow) and mark code noexcept,
  • decide when to use exceptions and when to prefer error codes such as std::optional or std::error_code.

1. Why not just return an error code?

Every non-trivial function can fail: a file will not open, memory runs out, user input is nonsense, a network read times out. You have two broad ways to report that failure back to the caller.

  • Error codes / sentinel values: the function returns a special value (-1, nullptr, false, an enum) that the caller is supposed to check.
  • Exceptions: the function throws an object; normal execution stops and control jumps to a handler.

The trouble with error codes is that they are easy to ignore. Nothing forces the caller to look at the return value, so a forgotten check silently corrupts the program. They also clutter every call site with if checks, and they cannot report failure from places that have no return channel, such as a constructor or an overloaded operator. Exceptions solve those problems: an unhandled exception cannot be silently dropped (it terminates the program instead), and the error type travels with a message and can carry data.

Rule of thumb, straight from the C++ Core Guidelines: use exceptions for errors that are rare and that the immediate caller usually cannot handle, and prefer return values for failures that are expected and local. We will sharpen this in section 7.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers