C++ 是否具有等效于 .NET 的 NotImplementedException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24469927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does C++ have an equivalent to .NET's NotImplementedException?
提问by becko
Does the standard library of C++ contain an exception equivalent to .NET's NotImplementedException?
C++ 的标准库是否包含与 .NET 的 NotImplementedException 等效的异常?
If not, what are the best practices to handle incomplete methods that I intend to complete later on?
如果不是,处理我打算稍后完成的不完整方法的最佳实践是什么?
采纳答案by dustyrockpyle
You can inherit from std::logic_error, and define your error message that way:
您可以从 std::logic_error 继承,并以这种方式定义错误消息:
class NotImplementedException : public std::logic_error
{
public:
virtual char const * what() const { return "Function not yet implemented."; }
};
I think doing it this way makes catching the exception more explicit if that's actually a possibility. Reference to std::logic_error: http://www.cplusplus.com/reference/stdexcept/logic_error/
我认为这样做可以更明确地捕获异常,如果这实际上是可能的。参考 std::logic_error: http://www.cplusplus.com/reference/stdexcept/logic_error/
回答by MatrixManAtYrService
In the spirit of @dustyrockpyle, I inherit from std::logic_error
but I use that class's string constructor, rather than overriding what()
本着@dustyrockpyle 的精神,我继承了std::logic_error
但我使用了该类的字符串构造函数,而不是覆盖what()
class NotImplemented : public std::logic_error
{
public:
NotImplemented() : std::logic_error("Function not yet implemented") { };
};
回答by Quentin
Since this is just a temporary exception that does not carry any application meaning, you can just throw a char const* :
由于这只是一个不带有任何应用意义的临时异常,您可以抛出一个 char const* :
int myFunction(double d) {
throw "myFunction is not implemented yet.";
}
回答by glampert
A good practice would be for your application to define its own set of exceptions, including one for unimplemented method, if that is needed. Make sure you inherit your exception type from std::exceptionso that callers of exception throwing functions can catch the error in a uniform way.
一个好的做法是让您的应用程序定义自己的一组异常,如果需要的话,包括一个用于未实现的方法的异常。确保您从std::exception继承了您的异常类型,以便异常抛出函数的调用者可以以统一的方式捕获错误。
Just one possible way to implement a NotImplementedException:
实现NotImplementedException 的一种可能方法:
class NotImplementedException
: public std::exception {
public:
// Construct with given error message:
NotImplementedException(const char * error = "Functionality not yet implemented!")
{
errorMessage = error;
}
// Provided for compatibility with std::exception.
const char * what() const noexcept
{
return errorMessage.c_str();
}
private:
std::string errorMessage;
};
回答by James
Here's my variation of this, which will show the function name and your own message.
这是我对此的变体,它将显示函数名称和您自己的消息。
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();
}
};