"You should only use try/catch. File.Exists() is a waste of time,
because you always _have_ to have the try/catch block anyway. Even if
File.Exists() returns true, the file could disappear before you can
actually open it and load it as an XDocument, and you could experience
other errors as well."
I want to know in common if it is better to _only_ catch exceptions
instead of verifying dependencies like existing files before an operation..
In many cases, preconditions can be ensured without exception
handling. For example, you would almost never need to catch an
ArgumentNullException. It is usually possible to *know* that an object
reference is not null at the time that object reference is used
without checking it and without error handling. And if you don't know
it for a fact, then you can always check to see if it's a null
reference. Thus there's no reason to catch the exception.
This is *never* the case for IO.
External things like files are a special case. The file system is out
of your control in a way that other things (like collection objects
for example) are not. There is no way to ensure that preconditions are
always met when dealing with files (or TCP connections, etc.).
I would say that in most cases it is not really helpful to call
File.Exists(), since it provides no assurances at all. A file can
exist, but still be unreadable, because of security issues, locking,
etc. A file can exist one second and be gone the next. A connection to
a network share could drop in the middle of reading the file. There
are many ways IO can fail, and a failure to handle those exceptions
properly is a bug.
In spite of this, there are a few cases when it might make sense to
call File.Exists(). For example, if the logic of your process depends
in some way on the existance of a file, such as a signaling file
created by a batch application. Or if a long-running process could
fail at the end because of the existance or non-existance of a file,
and options could be taken at the beginning of the process to mitigate
the error.
But if the file is expected to be there as part of the process, then
File.Exists() is pointless. If you're planning to open the file, then
checking its existance is pointless, because you have to handle the
other exceptions anyway. If the user has just selected the file in the
file-open dialog box, then calling File.Exists() is just plain nuts.