Core · Module 8
Exceptions and Error Handling
Report failures that callers cannot ignore, keep every object valid when throws happen, and choose between exceptions and error codes.
Returning -1 is easy to ignore, and checking it everywhere clutters the happy path. Exceptions separate the two: normal code reads straight down, and errors jump to a handler. This module shows how stack unwinding runs your destructors for you, why that makes RAII essential, and how to choose among exceptions, std::optional, error codes and C++23 std::expected for different failure contracts.
- 1Throwing and Catching: try, catch and throwPro — members onlySignal failures with exceptions, catch them by const reference in the right order, and keep throwing code safe with RAII.9 min
- 2Standard Exceptions and Custom Exception TypesPro — members onlyPick the right standard exception, derive focused custom types with noexcept what, and order catch clauses from most to least specific.9 min
- 3Exception Safety: Guarantees and RAII DisciplinePro — members onlyPromise basic, strong, or nothrow safety for mutating operations, and use RAII plus copy-and-swap to keep state coherent when throws occur.10 min
- 4Project: Failing Library Operations CleanlyPro — members onlyBuild a LibraryError hierarchy, validate a checkout before mutating, and commit with noexcept swaps so failures leave no residue.12 min
- 5C++23 std::expected: Returning Values or ErrorsPro — members onlyUse std::expected to return a successful value or a typed failure, compose operations, and choose it deliberately alongside exceptions and optional.14 min