Vishful thinking…

Proper use of ArgumentException ?

Posted in Uncategorized by viswaug on September 30, 2007

I have been paying real close attention to exceptions that are being thrown by third party libraries and the .NET framework to try to understand and be more specific about the exceptions that I need to raise in my applications. So, I noticed that when I set the Filter property of a FileDialog class with a value like ‘rpt’, an exception is raised at runtime because the value ‘rpt’ is not valid for the Filter property. But the interesting part about it is that it raises an ‘ArgumentException‘.  The message on the exception reads “Filter string you provided is not valid. The filter string must contain a description of the filter, followed by the vertical bar (|) and the filter pattern. The strings for different filtering options must also be separated by the vertical bar. Example: “Text files (*.txt)|*.txt|All files (*.*)|*.*”“. The stack trace reads that the exception occurred at “System.Windows.Forms.FileDialog.set_Filter(String value)“.

The MSDN documentation for the ArgumentException reads ‘The exception that is thrown when one of the arguments provided to a method is not valid’. But in this case, I am setting a property on a class and not passing an argument to a method. So, is a wrong exception being thrown by the .NET framework or is it a valid use of the ArgumentException. If it is a valid use of the ArgumentException, the documentation should definitely reflect it which I don’t think it currently does.

2 Responses

Subscribe to comments with RSS.

  1. Greg said, on December 3, 2007 at 1:28 am

    Hi Vish,

    It’s a good question. I have personally always thought of properties as convenience representations of methods. As shown in the error text, under the covers the actual property has been turned into the method set_Filter (there is a corresponding get_Filter too). So to answer your question, I do believe it is a proper use, but agree that the documentation should be re-phrased.

    Greg

  2. viswaug said, on December 3, 2007 at 1:38 am

    Hi Greg,

    I agree with what you are saying. But also, in my opinion, maybe adding an “InvalidPropertyException” wouldn’t be too bad either. It might make debugging just a little bit easier.

    Vish


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: