Utilizing exception hierarchies
Creating a class to encapsulate the details relating to a program error seems like a useful endeavor. In fact, the C++ Standard Library has created one such generic class, exception
, to provide the basis for building an entire hierarchy of useful exception classes.
Let’s take a look at the exception
class with its Standard Library descendants, and then how we may extend exception
with our own classes.
Using standard exception objects
The exception class is defined in the C++ Standard Library and is available merely by including the <exception>
header. The exception
class includes virtual functions with the following signatures: virtual const char *what() const noexcept
and virtual const char *what() const throw()
. These signatures indicate that derived classes should redefine what()
to return a const char *
with a description of the error at hand. The const
keyword after what()
indicates that these are const
member functions; they...