Here is my code:
public void ReadSomeFile(string filePath)
{
if (!File.Exists(filePath))
throw new FileNotFoundException();
var stream = new FileStream(filePath, ....)
.....
}
Should I throw an exception myself (see the File.Exists
check)? FileStream
will already throw FileNotFoundException
if the the file doesn't exist. What is good programming practice here? Code analysis says that we should validate our parameters. But if I am passing that parameter directly to another method (mine or someone else code) and that method will throw exception itself, then what is advantage of validating argument in my code?
FileNotFoundException
- in fact, it's just inviting race condition issues. Either you handle the exception, let it propagate, or wrap it in your own exception. This corresponds to "I know what to do with this", "I don't know what to do with this" and "I want to handle this higher on the stack" respectively.filePath
looks valid (i.e. absolute path, or at least does not containPath.GetInvalidFileNameChars()
)