bec*_*cko 32 c++ exception-handling
C++的标准库是否包含与.NET的NotImplementedException等效的异常?
如果没有,处理我打算稍后完成的不完整方法的最佳实践是什么?
Mat*_*ice 39
在@dustyrockpyle的精神,我继承,std::logic_error
但我使用该类的字符串构造函数,而不是覆盖what()
class NotImplemented : public std::logic_error
{
public:
NotImplemented() : std::logic_error("Function not yet implemented") { };
};
Run Code Online (Sandbox Code Playgroud)
dus*_*yle 24
您可以从std :: logic_error继承,并以这种方式定义您的错误消息:
class NotImplementedException : public std::logic_error
{
public:
virtual char const * what() const { return "Function not yet implemented."; }
};
Run Code Online (Sandbox Code Playgroud)
我认为这样做可以使异常更明确,如果这实际上是一种可能性.参考std :: logic_error:http://www.cplusplus.com/reference/stdexcept/logic_error/
这是我的变体,它将显示函数名称和您自己的消息。
class NotImplemented : public std::logic_error
{
private:
std::string _text;
NotImplemented(const char* message, const char* function)
:
std::logic_error("Not Implemented")
{
_text = message;
_text += " : ";
_text += function;
};
public:
NotImplemented()
:
NotImplemented("Not Implememented", __FUNCTION__)
{
}
NotImplemented(const char* message)
:
NotImplemented(message, __FUNCTION__)
{
}
virtual const char *what() const throw()
{
return _text.c_str();
}
};
Run Code Online (Sandbox Code Playgroud)