Geeks With Blogs

News



Add to Google

Tim Hibbard CEO for EnGraph software

I love using Interfaces.  They are great for three reasons:

  • They enforce that your code implements specific functionality
  • You can pass around interfaces in your code and any object that implements that interface can be used as a parameter
  • Visual Studio automatically populates the methods signatures in a class that implements you interface

I wish there was more an interface could do.  Like enforce a method to handle a specific type of exception.  Maybe it could be done with attributes.  Something that would look like this:

 

 

public interface IFileManipulation { [RequiredExceptionAttribute(System.IO.FileNotFoundException)] bool Delete(string FileName); [RequiredExceptionAttribute(System.IO.DriveNotFoundException)] bool Save(string FileName); } public class DataFileHelper : IFileManipulation { #region IFileManipulation Members public bool Delete(string FileName) { try { throw new System.Exception("The method or operation is not implemented."); } catch (System.IO.FileNotFoundException FileNotFoundException) { throw FileNotFoundException; } } public bool Save(string FileName) { try { throw new System.Exception("The method or operation is not implemented."); } catch (System.IO.DriveNotFoundException DriveNotFoundException) { throw DriveNotFoundException; } } #endregion }

 

I know you can enforce exception handling using FxCop, but it takes so long to set up and I like the automatic code population that interfaces provide.  Dave Donaldson is the attribute man, maybe he can provide some guidance.

 

Technorati tags: ,
Posted on Friday, April 27, 2007 10:44 AM .NET | Back to top


Comments on this post: Super Interfaces

# re: Super Interfaces
Requesting Gravatar...
Hmm, this seems similar (in essence) to the Java throws keyword. There's definitely two camps of thought about this construct, but Anders Hejlsberg had a good argument for not including them in C# ...

Have a look here http://www.artima.com/intv/anders.html' and perhaps read the part of the Interview about Checked Exceptions.
Left by Russ C. on Apr 30, 2007 6:19 AM

Your comment:
 (will show your gravatar)


Copyright © Tim Hibbard | Powered by: GeeksWithBlogs.net