Should I inherit from std::exception?
The main benefit is that code using your classes doesn't have to know exact type of what you throw
at it, but can just catch
the std::exception
.
Edit: as Martin and others noted, you actually want to derive from one of the sub-classes of std::exception
declared in <stdexcept>
header.
The problem with std::exception
is that there is no constructor (in the standard compliant versions) that accepts a message.
As a result I prefer to derive from std::runtime_error
. This is derived from std::exception
but its constructors allow you to pass a C-String or a std::string
to the constructor that will be returned (as a char const*
) when what()
is called.