Does C++ have an equivalent to .NET's NotImplementedException?

后端 未结 5 1669
别那么骄傲
别那么骄傲 2020-12-28 13:17

Does the standard library of C++ contain an exception equivalent to .NET\'s NotImplementedException?

If not, what are the best practices to handle incomplete methods

相关标签:
5条回答
  • 2020-12-28 14:08

    In the spirit of @dustyrockpyle, I inherit from std::logic_error but I use that class's string constructor, rather than overriding what()

    class NotImplemented : public std::logic_error
    {
    public:
        NotImplemented() : std::logic_error("Function not yet implemented") { };
    };
    
    0 讨论(0)
  • 2020-12-28 14:15

    Since this is just a temporary exception that does not carry any application meaning, you can just throw a char const* :

    int myFunction(double d) {
        throw "myFunction is not implemented yet.";
    }
    
    0 讨论(0)
  • 2020-12-28 14:15

    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::exception so that callers of exception throwing functions can catch the error in a uniform way.

    Just one possible way to implement a 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;
    };
    
    0 讨论(0)
  • 2020-12-28 14:17

    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();
        }
    };
    
    0 讨论(0)
  • 2020-12-28 14:19

    You can inherit from std::logic_error, and define your error message that way:

    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/

    0 讨论(0)
提交回复
热议问题