This is a picky thing and it is probably just my OCD flairing up but I was wondering why the standard exception class hierarchy is set up as it is.
exception
bad_alloc
bad_cast
bad_typeid
bad_exception
ios_base::failure
runtime_error
subclasses...
logic_error
subclasses...
Couldn't all the bad_* exceptions just be subclasses of something like lang_support_error? And ios_base::failure seems completely out of place.
Is there some historical or technical reasons the hierachy ended up like this?
Advantage 1: Separating Error Handling Code from "Regular" Code. Advantage 2: Propagating Errors Up the Call Stack. Advantage 3: Grouping Error Types and Error Differentiation.
The class at the top of the exception class hierarchy is the Throwable class, which is a direct subclass of the Object class. Throwable has two direct subclasses - Exception and Error. The Exception class is used for exception conditions that the application may need to handle.
Exception HierarchyAll exception and error types are subclasses of class Throwable, which is the base class of the hierarchy. One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. NullPointerException is an example of such an exception.
Structured exception handling (SEH) is a Microsoft extension to C to handle certain exceptional code situations, such as hardware faults, gracefully. Although Windows and Microsoft C++ support SEH, we recommend that you use ISO-standard C++ exception handling. It makes your code more portable and flexible.
If I remember correctly, the logic was:
logic_error
would be the equivalent of an assert
, but with less drastic behaviorruntime_error
would be the base of all othersHowever, as you noticed, it does not quite hold, even in the standard library itself.
The main issue I guess is subjectivity: is std::out_of_range
a logic_error
or a runtime_error
?
It's subjective...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With