exception hierarchy vs error enumeration
c++, exception
Solution
Simple rule of thumb:
- If you end up rethrowing exceptions after examining them, then you need a more finegrained exception hierarchy (Except for the rare case where the examination entails considerable logic).
- If you have Exception classes that are never caught (only their supertypes are), then you need a less finegrained exception hierarchy.
Problem
I read somewhere (can't find it now) that large exception hierarchies are a waste of time. The justification for this statement seemed sound at the time and the idea stuck with me. In my own code when I have a code base that can have a range of error conditions I use a single exception with an enumeration member to differentiate between them. If the case arises that I need to catch on one of these errors, I catch it, check the enum and rethrow if it was not what I expected. Ideally, this should be rare. I was working with exceptions again and the I had a reflective moment where I questioned my exception habits. I am curious what everyone else does and why? Hierarchy or one exception with data members. BTW, I am assuming you agree with the idea of exception vs error codes. I don't wish to open that can of worms.