1

C# has made a language feature of the NotImplementedException. It's added to a lot of auto-generated code, such as event handler stubs:

// Auto-generated
private void TextBox_MouseDown(object sender, MouseEventArgs e)
{
    throw new NotImplementedException();
}

What makes this exception

  • useful?
  • a good language feature?

It seems to me that the only person who should ever see that exception thrown is the developer of a piece of software - but that's what issue and TODO tracking systems are for.

  • It's only a good thing as far as its a very visible reminder that something isn't done. Far too often however, it's used to avoid fully implementing an interface, which should never be done because it breaks Liskov's substitution principle. – RubberDuck Jun 28 '16 at 22:29
  • 2
    @ArtOfCode That's still not a language feature, the Base Class Library is technically not part of the language. – svick Jun 29 '16 at 15:28
  • 3
    As Brian Kernighan once said (in a lecture I attended) -- "what runs, the code or the comments?" – Eric Lippert Jun 29 '16 at 22:03
  • 1
    Heh. @RubberDuck your wish came true ^ – ArtOfCode Jun 29 '16 at 22:05
  • 1
    I believe Eric Lippert kibozes Stack Overflow, showing up wherever his name is mentioned. And then there's people like me, who regularly check his activity feed to see what wisdom he's bestowed lately‚Ķ – user101289 Jun 30 '16 at 13:37
17

It allows the code to compile for your method stub (regardless of the method's return type), while you get around to putting in an implementation.

It also reminds you to put in the implementation, because it will throw the first time you try to MouseDown on that textbox. A thrown exception that says "This method is not implemented" is much better than clicking a textbox and wondering why nothing happens.

| improve this answer | |
7

There is an additional case which wasn't mentioned in the previous answers: mocks for unit tests.

A mock can need to implement only a small part of an interface, but to compile, it should declare all of them. The not implemented exception makes then a very clear difference between methods which are actually required by the test, but return nothing or a dummy value, and methods which aren't needed during the test.

| improve this answer | |
  • 1
    That would be NotSupportedException, not NotImplementedException. I note that NotSupportedException is also used by many BCL types like ReadOnlyCollection<T> and various Stream subclasses when it was commonplace (but not necessarily a good idea) for types to only partially implement an interface - this was before the advent of IReadOnlyList<T>, I note). – Dai Nov 5 '19 at 11:53
  • @Dai I disagree. NotSupportedException is used when implementing a method is optional and there is an explicit choice not to. NotImplementedException is used prior to any implementation decisions. With mocks, the programmer doesn't choose to not implement each unimplemented method. They haven't thought about it, and won't think about it. – ILMTitan May 6 at 22:49
  • @ILMTitan That's not my opinion though - Microsoft's own documentation for NotImplementedException says "a NotImplementedException exception should be synonymous with 'still in development.'" - there's no mention in the article suggesting it should be used for mocks and the "other exception types" section name-drops NotSupportedException. – Dai May 6 at 22:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.