1

Best practice question for error handling in C#?

In our system we will be writing out files, and with each file they will have a corresponding error file. The error file will get written every time even if there are not any errors.

I'm looking at the best way to handle the errors for this. Assuming when we go to write the main CSV file, we would get a number of errors. From permissions on the folder, to a parsing exception when turning a Datatable to a CSV.

If we get a permission error the error log file will fail as well. Would this be done in the exception? in another try catch (which would be bad.. I think).

We would also want to get the exception to place in the error file.

Some pseudo code below (without the try catch in the exception) or should this be broken down more and using methods, that contain their own Try / Catch that could bubble up the messages to log and write to files.

    Message.log("begin writing file");
        try {
           try { 
              // get data from DB
              // write to CSV file
              // all good, write Empty Error Log file and main file
           } 
           catch(Exception ex) {
              // Write Error File with ex.toCSV
              // write log message.
              Message.log("error : " + ex.ToString());
           }

        } catch (Exception ex) {
             var e = new Exception(string.Format("An unexpected error occurred for file.\r\n{0}", ex.Message));
             Message.log(e);
             throw e.
        }
       Message.log("all files written");

2 Answers 2

2

The problem is your logger. The logger should not throw errors. If there is any error in logger try to resolve it best in logger code, by changing the log location or by disabling the logging, or in extreme by stopping application. Logging is frequent operation, if once it fails it is going to fail again and again. It will be expensive putting try catch around the logging operation every time.

1
  • True the log shouldn't fail. I think I'm over thinking it, as I would be writing two files. The main file, and the .error file if the write fails on the main file it will fail on the error. And sense we are always writing the error file, no error file would mean an error (for which I should have logged with the logger).
    – chatters
    May 23, 2013 at 12:53
0

You only need one catch, provided you can make the message you are logging have a single format. You can also simply rethrow the caught exception, rather than creating a new one.

The following code is semantically equivalent to your existing code:

Message.log("begin writing file");
try 
{
  // get data from DB
  // write to CSV file
  // all good, write Empty Error Log file and main file
}  
catch (Exception ex) 
{
     Message.log(e);
     throw;
}
Message.log("all files written");
2
  • but would I write the log file in the catch? and am I able to throw a error from there if the error file cannot write? (error file must always write unless its absolutely unable to.)
    – chatters
    May 22, 2013 at 20:44
  • Then you might need more intricate tests in your try and/or catch blocks, but nesting exceptions is seldom needed, if ever. May 22, 2013 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.