Understanding the hierarchy of exception classes in C++ is crucial for effective exception handling. The standard library defines a set of exception classes that inherit from std::exception
, the base class for all exceptions in C++.
At the root of the C++ standard exception class hierarchy is std::exception
. It is an abstract class that defines a virtual what()
method. This method returns a const char*
and is meant to provide an explanatory string when an exception is caught.
The standard library provides several classes derived from std::exception
to represent different error conditions:
-
std::logic_error
: An exception that theoretically can be detected before the program runs; includes:std::invalid_argument
: Invalid argument passed to a function.std::domain_error
: Mathematical domain error.std::length_error
: Operation exceeds a size limit.std::out_of_range
: Accessing an element out of range.
-
std::runtime_error
: An exception that occurs while the program is running; includes:std::range_error
: Error when a value does not fall within the expected range.std::overflow_error
: Arithmetic overflow error.std::underflow_error
: Arithmetic underflow error.
-
std::bad_alloc
: Thrown by new when memory allocation fails. -
std::bad_cast
: Thrown by dynamic_cast when it fails in a dynamic cast to a reference type. -
std::bad_typeid
: Thrown by typeid for null pointers and incomplete types.
The specific classes derived from std::exception
allow a program to catch exceptions corresponding to specific error conditions. This is done by writing catch blocks for each exception type:
try {
// Code that may throw exceptions
} catch (const std::logic_error& e) {
// Handle logic errors
} catch (const std::runtime_error& e) {
// Handle runtime errors
} catch (const std::exception& e) {
// Handle all other std::exception-based errors
}
You can also derive your own exception classes from any of the standard exception classes. This allows you to throw and catch exceptions specific to your application's domain while still fitting into the overall exception hierarchy:
class MyCustomException : public std::runtime_error {
public:
MyCustomException(const std::string& msg) : std::runtime_error(msg) {}
};
The std::exception
class hierarchy provides a structured way to handle errors in C++. By using the specific exception classes, or extending them, programmers can write clear and maintainable exception handling code. It is a fundamental part of writing robust C++ programs that can gracefully handle exceptional circumstances.