Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have read at many places including this - http://msdn.microsoft.com/en-us/library/seyhszts.aspx - that you should use exception handling when something is truly exceptional.

The .Net File.Open function throws FileNotFoundException, etc. instead of returning error codes. Now it can frequently happen that the filename passed does not exist. This does not appear to me a truly exceptional situation.

Why may be the reason behind File.Open not following exception handling best practices?

share|improve this question
add comment

marked as duplicate by Karl Bielefeldt, MichaelT, GlenH7, Jesse C. Slicer, Glenn Nelson Feb 8 '13 at 22:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

It is expected that you call File.Open(...) only on files that you know (or expect) to exist. If you don't know whether the filepath passed exists or not, you're expected to call File.Exists(...) before trying to open it.

If you try to open a file you expect to exist, but it doesn't, that's an exception.

share|improve this answer
1  
But a contrary viewpoint can be read here: stackoverflow.com/questions/673654/… –  Eric King Feb 8 '13 at 21:35
    
As far as I'm concerned, using File.Exists to avoid opening non-existent files is just broken. –  Winston Ewert Feb 8 '13 at 22:34
    
@WinstonEwert You're not alone. :-) That's why I linked to the other thread in my comment. –  Eric King Feb 8 '13 at 22:34
add comment

The way exceptions are handled in .NET is materially different than the way they are handled in, say, Java. Exceptions in Java demand more from the programmer than .NET does, and so the argument could be made that you reserve exception handling in Java for the truly exceptional cases.

In .NET, an exceptional condition is one that you cannot reasonably recover from without user intervention. If the user passes the name of a file to a method which expects the file to exist, but the file doesn't exist, that's an exceptional condition.

As a programmer, you still have the option of checking for the existence of the file first, before calling the method that requires it.

In any case, the guidance that Microsoft offers seems relatively straightforward:

If the event is truly exceptional and is an error (such as an unexpected end-of-file), using exception handling is better because less code is executed in the normal case. If the event happens routinely, using the programmatic method to check for errors is better.

share|improve this answer
add comment

Eh? File.Exists erm... exists. If the filename you pass in does not exist after the check, that is exceptional.

share|improve this answer
3  
Are you sure you didn't mean to post this as comment? –  Robert Harvey Feb 8 '13 at 21:27
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.