C++ supports exceptions for error propagation—unlike error codes alone in C, though many teams still use std::optional or expected-style returns for control flow.
try / catch / throw
try {
throw std::runtime_error("failed");
} catch (const std::exception& ex) {
std::cerr << ex.what() << "\n";
}
RAII ensures destructors run during stack unwinding—locks and files still release.
Important interview questions and answers
- Q: Exceptions vs error codes?
A: Exceptions separate error path from happy path; codes are predictable in real-time/embedded contexts where exceptions may be banned. - Q: noexcept?
A: Promises a function won't throw—enables optimizations and documents contracts.
Self-check
- What runs during stack unwinding?
- Base type to catch standard exceptions?
Tip: RAII means destructors still run during exception unwinding—unlike error-code-only paths in C where cleanup must be manual.
Interview prep
- RAII and exceptions?
Stack unwinding runs destructors—resources acquired via RAII still release on throw.
- Exceptions vs error codes?
Exceptions separate error paths; error codes suit real-time/embedded contexts where exceptions may be banned.