61

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 that I intend to complete later on?

a06e
  • 18,594
  • 33
  • 93
  • 169

5 Answers5

69

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") { };
};
MatrixManAtYrService
  • 8,023
  • 1
  • 50
  • 61
35

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/

dustyrockpyle
  • 3,184
  • 17
  • 12
4

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.";
}
Quentin
  • 62,093
  • 7
  • 131
  • 191
  • 25
    I think any exception not derived from std::exception is evil (exceptions might apply) –  Jun 28 '14 at 19:04
  • 2
    @DieterLücking Was that last "exception" a pun ? :p I agree with you, but I think this is one of the special cases. Such an "exception" is no exception really, just a way to terminate the program as soon as an unimplemented function is called. I wouldn't create a full-fledged class for that. – Quentin Jun 28 '14 at 19:17
  • Is `throw std::exception("myFunction is not implemented yet.")` safer? – a06e Jun 28 '14 at 19:22
  • @becko define "safer". If you're thinking about memory leaks, neither will suffer from them. – Quentin Jun 28 '14 at 19:24
  • It might be a corner case here, but that exception is as good as an exit call - which might upset users of the function. –  Jun 28 '14 at 19:25
  • 3
    @DieterLücking This has the advantage of including a descriptive string, so it would rather be an assert(("myFunc...",0));, albeit more elegant. As for users... Do users really protect their programs against non-existing functions at each call site, with alternate non-fatal code paths ? – Quentin Jun 28 '14 at 19:29
  • @Quentin I do this not so users can catch the exception. I do it because when you're reading the code it is much more clear to see a function throw a "NotImplemented" exception instead of needing to read the message string – Jordan Nov 30 '18 at 19:59
4

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();
    }
};
Dilawar
  • 5,438
  • 9
  • 45
  • 58
James
  • 1,973
  • 1
  • 18
  • 32
3

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;
};
glampert
  • 4,371
  • 2
  • 23
  • 49