Reputation: 37632
So what is better? To use construction like this:
if (File.Exist(fileName))
{
// do something with file...
}
of just
try
{
// do something with file.
}
catch(Exception ex)
{
}
Does it cost a lot to use method File.Exist()?
Thank you!
Upvotes: 4
Views: 845
Reputation: 151674
Depends on your program flow and the actions you're performing. If you expect the file to exist, you can rely on exception handling, since your program cannot continue if it doesn't and the exception most probably needs to be handled higher up in the call chain.
Otherwise, you'll get the True|False|FileNotFound
return code madness, if the method in question is something like ReadFile()
.
Using File.Exists to "safely" open a file is pretty useless. Consider this:
public String ReadFile(String filename)
{
if (!File.Exists(filename))
{
// now what? throw new FileNotFoundException()? return null?
}
// Will throw FileNotFoundException if not exists, can happen (race condition, file gets deleted after the `if` above)
using (var reader = new StreamReader(filename))
{
return reader.ReadToEnd();
}
}
One could say you'd want to check if a file exists if you want to append data to it, but the StreamWriter constructor has an overload with an append
parameter, which will let the writer create the file if it doesn't exist and append to it if it does.
So, perhaps the question could better be: what valid use cases exist for File.Exists
? And luckily that question has already been asked and answered.
Upvotes: 1
Reputation: 68440
Exceptions are not supposed to be used to handle the flow of your application, the idea is to avoid exceptions and not to expect them as normal part of the execution flow.
For 99.999% of the applications, if there is any performance difference, won't be appreciable. If the file should be there and not finding is a exceptional scenario, you could use the try catch
block otherwise I'd say you should go for the File.Exist
approach.
Upvotes: 5
Reputation:
The former has a race condition: another process may remove the file after File.Exists
has returned true, but before you open it. The latter does not. Even if you check beforehand, you should still catch an exception if you want to ignore nonexistant files.
So it should be either
if (File.Exists(fileName))
{
try
{
// ...
}
catch (FileNotFoundException)
{ }
}
or
try
{
// ...
}
catch (FileNotFoundException)
{ }
The former duplicates the check, which could be slow if the file is on a network share, the latter raises an exception (which gets handled) for a non-exceptional condition, complicating debugging. Both have their merits. Personally, I generally opt for the second, but either is okay.
Upvotes: 11
Reputation: 4962
The first case-
if (File.Exist(fileName)) //A single statement to check and proceed.
While the later method of exception handling involves-
An object
of the exception class to be created and passed to the corresponding catch block if exception is raised.
I would prefer the first method since using exception handling for handling the flow of execution isn't a good idea.
Upvotes: 0
Reputation: 18084
First I'd check if the file exists and throw an exception if it doesn't exist.
After that I'd use a try-catch block to handle other exceptions that may be thrown (permissions, etc)
if (!File.Exist(fileName))
{
throw new ArgumentException("filename");
// or throw new FileNotFoundException("filename");
}
try
{
// do something with file.
}
catch(Exception ex)
{
}
Upvotes: 0