2014-01-16

Recently I got got an exception that I wasn't expecting because it wasn't documented in MSDN that it can be thrown by an particular constructor. So here is the C# line that threw exception:

filePath here is the string that should contain full path to a certain file. The problem was that my "filePath" variable was actually a path to the folder and not the file. Because of this the constructor StreamReader(filePath) threw:

Ok so this was obviously a bug and I've fixed it by passing a correct path... but looking at the MSDN docs for StreamReader(string) I don't see any mentioning of this exception. Under exception section there are:

ArgumentException - path is an empty string ("").

ArgumentNullException - path is null.

FileNotFoundException - The file cannot be found.

DirectoryNotFoundException - The specified path is invalid, such as being on an unmapped drive.

IOException - path includes an incorrect or invalid syntax for file name, directory
name, or volume label.

Thinking some more about this issue, I guess the exception thrown should actually be IOException and not UnauthorizedAccessException. Is this a bug in .NET Framework? The problem is that I had IOException handler in place that notifies user about invalid file path and continues application workflow without crashing. This UnauthorizedAccessException crashed my application because it was unhandled.

How should I deal with this kind of issues? I think I encounter similar problem of undocumented exceptions in the past but this one really motivated me to research this issue and ask question here.

Show more