Skip to content
Module 6

Error Handling & Safety

Write robust C++ code with proper error handling — exceptions, RAII safety guarantees, sum types, and defensive programming patterns.

Exceptions & Error Strategies

  1. Exceptions: throw, try, catch

    Understand the C++ exception mechanism — stack unwinding, the standard exception hierarchy, custom exceptions, and when to prefer exceptions over error codes.

  2. Exception Safety Guarantees

    Learn the three levels of exception safety — basic, strong, and no-throw — and how RAII and copy-and-swap provide them.

  3. Error Handling Strategies: Choosing the Right Approach

    Compare exceptions, error codes, and std::expected — learn when each approach is appropriate and how to handle errors in constructors, destructors, and multithreaded code.

Type Safety & Defensive Programming

  1. std::optional, std::variant & std::expected

    Use C++17/23 sum types to represent nullable values, type-safe unions, and value-or-error returns — eliminating entire classes of bugs at compile time.

  2. Type Safety, Assertions & Contracts

    Leverage the C++ type system to prevent bugs at compile time — strong enums, the newtype pattern, static_assert, runtime assertions, and the upcoming contracts feature.

  3. Understanding & Avoiding Undefined Behavior

    Learn what undefined behavior is, why it exists, how compilers exploit it for optimization, and how to detect it with sanitizers and compiler warnings.