Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reasons for structure of standard exception hierarchy

Tags:

c++

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?

like image 591
ValenceElectron Avatar asked May 16 '11 16:05

ValenceElectron


People also ask

What are the advantages of exception hierarchy?

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.

Can you explain the hierarchy of exception handling classes?

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.

What is exception exception hierarchy?

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.

What is structured 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.


1 Answers

If I remember correctly, the logic was:

  • logic_error would be the equivalent of an assert, but with less drastic behavior
  • runtime_error would be the base of all others

However, 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...

like image 70
Matthieu M. Avatar answered Nov 04 '22 09:11

Matthieu M.