In C++ should I use std::runtime_error
to indicate some sort of error occurred, or should I create custom exceptions that inherit from std::runtime_error
so I can better handle them.
For example, if I got input from the user somehow, which would be better:
if (inputInvalid)
{
throw std::runtime_error("Invalid input!");
}
Versus...
class invalid_input
: public std::runtime_error /* or should I inherit from std::exception? */
{
public:
invalid_input()
: std::runtime_error("Invalid input!")
{
};
};
-------------------------------------------------------
if (inputInvalid)
{
throw invalid_input();
}
Which is considered better use of exception handling/which if better practice?