Exceptions in C++
From the C++ cheat sheet · Exceptions · verified Jul 2026
Exceptions
throw, try/catch, and noexcept.
cpp
// Quick Reference
try {
throw std::runtime_error("boom");
} catch (const std::exception& e) {
std::cerr << e.what();
}💡 Catch by const reference (const std::exception&) to avoid slicing and copies.
⚡ Order catch blocks most-derived first - a base std::exception catch would shadow the rest.
📌 Derive custom exceptions from std::exception so generic handlers can catch them.
🟢 RAII makes exceptions safe - resources release during stack unwinding automatically.
exceptionserror-handling