cpp
C++ Standard Library Exceptions
Exception types defined in the C++ Standard Library (<stdexcept>, <new>, <typeinfo>, etc.), thrown by the runtime and standard library operations. All standard exceptions derive from std::exception.
20 codes
· All codes 20 codes
- std--bad_alloc std::bad_alloc Thrown by operator new and operator new[] when memory allocation fails. Can be avoided by using the nothrow variant: new(std::nothrow) T, which returns nullptr on failure instead.
- std--bad_cast std::bad_cast Thrown by dynamic_cast when the cast to a reference type fails because the object is not of the target type. Casting to a pointer type returns nullptr instead of throwing.
- std--bad_function_call std::bad_function_call Thrown by std::function::operator() when the function wrapper holds no callable target (i.e., the std::function object is empty).
- std--bad_optional_access std::bad_optional_access Thrown by std::optional::value() when the optional object does not contain a value. Use optional::has_value() or the dereference operator (*opt) to avoid the throw.
- std--bad_typeid std::bad_typeid Thrown by the typeid operator when it is applied to a null pointer of a polymorphic type.
- std--bad_variant_access std::bad_variant_access Thrown by std::get<T>(variant) or std::get<I>(variant) when the variant does not hold the requested alternative. Use std::holds_alternative<T> to check before accessing.
- std--bad_weak_ptr std::bad_weak_ptr Thrown by the std::shared_ptr constructor that accepts a std::weak_ptr when the weak pointer has expired (the managed object has been destroyed).
- std--domain_error std::domain_error Thrown when a mathematical function is called with an argument outside its domain, such as passing a negative value to a function that requires a non-negative input.
- std--exception std::exception The base class for all standard C++ exceptions. Provides the virtual what() method that returns a human-readable description of the error. Custom exception classes should inherit from this or one of its subclasses.
- std--filesystem--filesystem_error std::filesystem::filesystem_error Thrown by std::filesystem functions when an operation fails. Carries up to two paths involved in the failing operation and a std::error_code with the underlying OS error.
- std--invalid_argument std::invalid_argument Thrown when a function receives an argument that has an inappropriate value, such as passing a string with invalid characters to std::stoi or constructing a std::bitset with an invalid string.
- std--ios_base--failure std::ios_base::failure Thrown by I/O stream operations when a stream error flag (failbit or badbit) is set and exceptions are enabled on that stream via stream.exceptions().
- std--length_error std::length_error Thrown when an operation would produce an object exceeding its maximum allowable size, such as calling std::string::resize() or std::vector::reserve() with a size larger than max_size().
- std--logic_error std::logic_error Base class for exceptions representing errors in program logic that could in principle be detected at compile time or before execution. Subclasses include invalid_argument, out_of_range, length_error, and domain_error.
- std--out_of_range std::out_of_range Thrown when an argument value is not in the expected range, such as accessing std::vector::at(), std::string::at(), or std::bitset::test() with an invalid index.
- std--overflow_error std::overflow_error Thrown when an arithmetic overflow occurs, such as when the result of a mathematical operation exceeds the representable range of the result type.
- std--range_error std::range_error Thrown when an internal computation produces a result outside the range that can be represented, typically from mathematical functions.
- std--runtime_error std::runtime_error Base class for exceptions representing errors that can only be detected at runtime. Subclasses include range_error, overflow_error, and underflow_error.
- std--system_error std::system_error Thrown when an OS-level or low-level library operation fails. Wraps a std::error_code which carries both the numeric error value and the error category (e.g. std::system_category for POSIX errno values).
- std--underflow_error std::underflow_error Thrown when an arithmetic underflow occurs, such as when a floating-point result is too small (close to zero) to be represented in the result type.