I have some special exception cases that I want to throw and catch, so I want to define my own exception classes.
What are the best practices for that? Should I inh
Yes, it's good practice to inherit from std::runtime_error
or the other standard exception classes like std::logic_error
, std::invalid_argument
and so on, depending on which kind of exception it is.
If all the exceptions inherit some way from std::exception
it's easy to catch all common errors by a catch(const std::exception &e) {...}
. If you have several independent hierarchies this gets more complicated. Deriving from the specialized exception classes makes these exceptions carry more information, but how useful this really is depends on how you do your exception handling.