Author image

Exception Class Hierarchies in C++


How to program exceptions, manage error codes etc. That is the question for today.

Luckily c++ exceptions are very standard in their programming. The strategy is to create a base exception class with a name of your choosing (KeyException was mine) and then you can create subclass exception classes, nested in the various principal classes of your codebase. For example, in a game engine project you can create GraphicsException, WindowException, KeyboardException all inherit from KeyException and override the getType and what() functions to provide meaningful unambiguous errors.

Many people confuse exceptions with error codes and asserts. Which one to use and when?

There is no hard and fast rule, but I'll give you my (and many others) rule of thumb: Assertions should be used for...

1