AJ Warnock

This Page Left intentionally Blank
posts - 36, comments - 7, trackbacks - 8

My Links

News




Archives

Post Categories

Developer Blogs

Development Community

NotImplementedException?

Well, I successfully avoided making an entry for the entire Month of April!  Not intentionally, but as often happens work, family and other responsibilities have maliciously absconded with my time.  So, I will try to do better in the future...

So, quite a while ago at the local .Net Developers Group a speaker was performing a demo using Visual Studio 2005.  During this presentation, he quickly right mouse clicked on an interface definition and selected Implement Interface.  A curious thing occurred after this.

As expected the designer created all of the appropriate methods of the interface and even surrounded them with appropriately named regions.  And, also as expected, the designer was not able to actually implement the methods.  Instead, it replaced the implementation with an exception throw.  What was the curious part was that the exception thrown was a plain old base exception (System.Exception).  Thanks David Hayden. for noticing this and letting me blog it…

Not that this is really a bad implementation, since the developer needs to go and implement this functionality.  In fact I think it is great that it throws an exception so that I know when I forget to implement one.  What I find curious is that it did not throw a different exception like the not implemented (System.NotImplementedException).  I even thought that it might be appropriate to use the Not Supported Exception (System.NotSupportedException).  Either one of these would seem more appropriate than just a plain ol’exception.

At first, I assumed that these exceptions were not available in the smaller footprint mobile libraries; but that proved to be inaccurate.  So, I am not sure why the Visual Studio Designer does not automatically put these exceptions in place.  Regardless, I believe that it is a good practice to immediately change these auto-generated exceptions to System.NotImplementedException's.  This will allow you to easily implement find any methods accidentally created without an implementation.  It will also allow you to at run-time detect this condition and handle the exception in a more appropriate manner.

So, the next time you use implement interface or another of the Visual Studio code generation designers, take a look at the exception throws and if you’re not going to implement the method or not supporting it change the exception object created.  In the meantime, it might be a good idea to take a look at all of the many other exceptions available in the .Net 2.0 Framework.

Until next time....

Print | posted on Monday, May 01, 2006 8:32 AM |

Comments have been closed on this topic.

Powered by: