Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log exceptions in Windows Forms Application

I read a lot about how bad catching base Exceptions is and I have to confess that I did it also:

try{
    ...
}
catch (Exception exception){
    MessageBox.Show(exception.Message, "Error!");
    MyLogger.Log(exception.Message);
}

Now I would like to do it right and have some questions about it:

  1. Which exceptions should I catch (for example FileNotExists for file manipulation, but what for TableAdapter or ReportClass (CrystalReports))
  2. Where can I see a list of exceptions, that an objects can throw (for example TableAdapter)
  3. Where in Windows Forms Application can I set a static method, which will log any exception to a file for example
  4. Any other suggestions?
like image 694
sventevit Avatar asked Nov 17 '09 15:11

sventevit


1 Answers

  1. Catch whichever exceptions you can reasonably handle. For example, if you're trying to open a file for writing, you should expect that maybe the file is marked read-only, so that would throw an exception. But in the same situation you wouldn't try to catch a null argument exception, because that would be due to programmer error.

  2. They should be found in the function reference in MSDN (you'll have to look it up on each one). For user-defined functions, you'll have to go digging, unless there is additional documentation or summary commentary.

3, 4. Consider using a logging library for .NET

like image 159
Jon Seigel Avatar answered Oct 05 '22 06:10

Jon Seigel