How to log exceptions in a Windows Forms application

I read a lot about how bad the Exception base trap is, and I must admit that I did this as well:

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

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

  • What exceptions should I catch (e.g. FileNotExists for file manipulation, but what for TableAdapter or ReportClass (CrystalReports))
  • Where you can see the list of exceptions that can be selected by objects (for example, TableAdapter)
  • Where in a Windows Forms application I can set a static method that will log any exception in a file, like
  • Any other suggestions?
+4
source share
6 answers
  • Catch those exceptions that you can reasonably handle. For example, if you are trying to open a file for writing, you should expect that perhaps the file will be marked as read-only, so this will throw an exception. But in the same situation, you would not try to catch the exception of the null argument, because this would be due to a programmer error.

  • They should be found in the function link in MSDN (you will have to search for it on each of them). For custom functions you will need to dig if there is no additional documentation or comment comments.

3, 4. Consider using the log library for .NET .

+7
source

I have one thing to add. If you just want to throw an exception without affecting the program flow, you can always do this:

 try { ... } catch (Exception exception) { MyLogger.Log(exception.Message); throw; } 
+5
source
  • What you decide, what exceptions the application logic can expect from recovery.
  • Exceptions cause method calls, not objects. In Visual Studio, Intellisense explanations tell you which exceptions are thrown by the object (assuming the XML documentation describes which exceptions the method throws.
  • Instead of using a static method, respond to the Application.ThreadException event. The given link contains examples. MSDN
+2
source

You can set an event for unhandled exceptions in the application event file

(there is a sample VB here, but I hope you get the point)

 Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException End Sub 

You can find application events in the parameters of your project.

+2
source

You should only catch exceptions that you can do something about, really.

This rule is. I usually have a try / catch around my Program.Main in case the exception gets to the top and requires registration. You can also handle the CurrentDomain_UnhandledException event if exceptions are thrown in threads other than the UI thread (assuming you're multi-threaded).

+1
source

In response to "4. Other suggestions?":

In your sample code, a message box is displayed before writing the exception. I would recommend registering an exception before displaying the message, in case the user sees an error message, panic and goes on vacation without clicking "OK". This is a minor thing, but message boxes block the program indefinitely and should be used with discretion!

+1
source

All Articles