Search code examples
c#debuggingcrash-dumps

How to identify problem when program crashes without showing error?


Please let me know what steps I need to follow when my application crashes and closes showing the dialog containing "Don't send" and "Send error report" buttons.

What can I possibly do other than looking at the event viewer to solve this?

Thanks


Solution

    1. You could add a try/catch/finally construct around your Main() entry method's body.

    2. For WinForms, you can add a ThreadException handler, just before Application.Run(), to catch exceptions thrown in WinForms UI event handlers:

      Application.ThreadException +=
         new ThreadExceptionEventHandler(Application_ThreadException);
      
    3. All other unhandled exceptions can be caught using:

      AppDomain.CurrentDomain.UnhandledException +=
         new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
      

      But it's worth mentioning that this only allows you to log/report the exception - you cannot prevent the application from closing once you exit this final handler.

    4. Visual Studio can also be configured to break on first chance exceptions, and external debuggers (like WinDbg with managed SoS extensions) can catch first-chance exceptions too (http://www.codeproject.com/KB/debug/windbg_part1.aspx).

    Also, use a logging framework like log4net to add useful logging to your app and dump exception information before the application closes.