I just want to learn something new here about my code.
I have the following function ,is it the optimized way to return if I had a problem during writing to a file? or there is a better way doing so..
public static bool WriteFile(ByteBuffer data , String fileName, bool append)
{
var writer = new StreamWriter(fileName, append);
var errorVal = true;
try
{
writer.Write(data);
writer.Flush();
}
catch (Exception ex)
{
errorVal = false;
}
finally
{
writer.Dispose();
}
return errorVal;
}
If you really want to eat all exceptions and just return a
bool, I would rather do it like this:That said, if it was my own code, I would probably not catch the exception at all in here, but rather leave that up to the calling code: