Jos*_*osh 5 c++ coding-style exception-handling runtime-error exception
我应该用C++ std::runtime_error来表示发生了某种错误,或者我应该创建继承的自定义异常,std::runtime_error以便我可以更好地处理它们.
例如,如果我以某种方式从用户那里得到了输入,那会更好:
if (inputInvalid)
{
    throw std::runtime_error("Invalid input!");
}
与...
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();
}
哪个被认为是更好地使用异常处理/如果更好的做法?
我只会在以下两种情况之一中对一个标准异常类进行子类化:
否则,没有多大意义.特别是如果其他人必须与您的代码交互并想知道为什么有一个自定义异常类不执行任何操作,标准异常就不会.