C++20 sections · 1024 units
Open in Course

Standard Exception Classes

Built-in error types

C++ provides a hierarchy of exception classes in . All inherit from std::exception. std::runtime_error: for errors detectable only at runtime (file not found, network failure).

std::logic_error: for programming errors (invalid argument, out of range). Specific types like out_of_range, invalid_argument, and overflow_error get from these. Using specific types lets catch blocks handle different errors differently.

Always catch by const reference: catch (const runtime_error& e). This avoids copying and slicing (losing derived class information).