Various exception classes can be derived from a common base class, as we discussed in Section 17.2, when we created class DivideByZeroException
as a derived class of class exception
. If a catch
handler catches a reference to an exception object of a base-class type, it also can catch
a reference to all objects of classes publicly derived from that base class—this allows for polymorphic processing of related exceptions.
Error-Prevention Tip 17.8
Using inheritance with exceptions enables an exception handler to catch related errors with concise notation. One approach is to catch each type of reference to a derived-class exception object individually, but a more concise approach is to catch pointers or references to base-class exception objects instead. Also, catching pointers or references to derived-class exception objects individually is error prone, especially if you forget to test explicitly for one or more of the derived-class reference types.