What are the pros and cons of using Exceptions vs. Error Codes?
Last updated
Exceptions cannot be silently ignored, keep the happy path clean, and work in constructors and operators. They cost a lot when thrown (stack unwinding) and make control flow non-local, requiring RAII-based code.
Error codes are cheap, deterministic and explicit at each call site. They are easy to ignore (unless
[[nodiscard]]) and clutter code with manual checks and propagation.Use exceptions for truly exceptional failures, and codes or
std::expected/std::optionalfor routine, expected outcomes.