Question

Once again, I'm struggling to understand an undocumented behavior of the method GetFiles in System.IO.Directory (this is going to make me crazy). While running some routine tests, I found out to my surprise that a call to this method was throwing an exception of type NotSupportedException, which is not mentioned in the MSDN page about GetFiles, in the list where the other exceptions are.

For example, this call in C#:

Directory.GetFiles(@"XY:");

throws a NotSupportedException. My first impression was that the documentation page is missing a piece of information, but after thinking again, I'm not really sure. I mean I've always been assuming that all exceptions I need to care about when using the .NET Framework classes are well-known and documented. For example, I never put a handler for a FileNotFoundException when I'm joining strings because I don't expect this exception to occur there.

Is my assumption wrong? Should I expect at least certain exceptions to potentially pop out of anywhere like an OutOfMemoryException? Any statement I need to read?

Was it helpful?

Solution

There are certainly some types of exceptions that can occur at any time without warning (a good example would be the classic StackOverflowException).

I 'd call this particular case a documentation bug though. The documentation for NotSupportedException does mention the System.IO classes in general, and it also says that the HRESULT value COR_E_NOTSUPPORTED translates to said exception type. I 'm not sure if it's reasonable to ask that every possible error result from PInvoked code be clearly listed as a possible exception type, but that's what seems to be happening here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top