8

I'm doing the exercises from Stroustrup's C++ Programming Language 4th Edition. One of the tasks is formulated like this:

Consider using a class Exception as the base of all classes used as exceptions. What should it look like? How should it be used? What good might it do? What disadvantages might result from a requirement to use such a class?

The answer looks pretty much like std::exception, save for the disadvantages part - the only one I can imagine is the cost of __vptr which is usually considered negligible. What am I missing here?

sigil
  • 815
  • 8
  • 16
  • 1
    This is an opinion-based question and depends on how you view the usage of exceptions. The disadvantage he was thinking of when he wrote this is probably that it allows consumers to write catch-all code that doesn't actually address the exceptional circumstance. – Collin Dauphinee Nov 17 '16 at 21:08

1 Answers1

1

The disadvantage is that if you try to catch an exception which is at the bottom of the inheritance tree, you will catch all exceptions derived from that one, and some of them might indicate things very different from what you were expecting.

Even worse, a developer who used a lot of functions which may throw exceptions, and doesn't know what some of them mean, might simply catch the base class, Exception, and just return an unspecific Exception instance when encountering errors, making it difficult to debug the code, and possibly causing the whole program to fail just because of an easily fixable exception which shouldn't cause any problems.