Handling files (opening) is an activity particularly prone to error.
If you were to write a function to do this (although trivial), what is the best way to write it in wrt handling errors?
Is the following good?
if (File.Exists(path))
{
using (Streamwriter ....)
{ // write code }
}
else
// throw error if exceptional else report to user
Would the above (although not syntactially correct) a good way to do this?
First you can verify if you have access to the file, after, if the file exists and between the creation of the stream use a try catch block, look:
So:
Or you can verify all things with the try catch, and create the stream inside the try catch.