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.