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.
9
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.
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.
3