What are the disadvantages of having one base class for all exceptions?

I do the exercises from Stroustrup C ++ Programming Language 4th Edition. One of the tasks is formulated as follows:

Consider using the Exception class as the base of all classes used as exceptions. How will it look like? How to use it? What is the use of this? What disadvantages can arise from the requirement to use such a class?

The answer looks something like std::exception , with the exception of some of the flaws - the only thing I can imagine is the cost of __vptr , which is usually considered insignificant. What am I missing here?

+8
c ++ exception-handling base-class
source share
1 answer

The disadvantage is that if you try to catch the exception located at the bottom of the inheritance tree, you will understand all the exceptions derived from this, and some of them may indicate things that are very different from what is expected.

Worse, a developer who has used many functions that can throw exceptions and does not know what some of them mean can simply catch the base class, the exception, and simply return a non-specific instance of Exception when errors occur, which makes it difficult to debug the code and possibly leads the entire program crashes only due to an easily fixed exception, which should not cause any problems.

+1
source share

All Articles