title: How to catch all unhandled exceptions in WinForms application correctly
body: <p>I want to set the handler method for all unhandled exceptions from any threads in my WinForms application. I don't create any application domains by myself.</p>
  
  <p>According to <a href="https://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx" rel="nofollow noreferrer">UnhandledException</a> documentation, I need to set <code>UnhandledExceptionMode.ThrowException</code> mode via <code>Application.SetUnhandledExceptionMode</code> method to catch main thread's exceptions too:</p>
  
  <blockquote>
    <p>In applications that use Windows Forms, unhandled exceptions in the
    main application thread cause the Application.ThreadException event to
    be raised. If this event is handled, the default behavior is that the
    unhandled exception does not terminate the application, although the
    application is left in an unknown state. In that case, the
    UnhandledException event is not raised. This behavior can be changed
    by using the application configuration file, or by using the
    Application.SetUnhandledExceptionMode method to change the mode to
    UnhandledExceptionMode.ThrowException before the ThreadException event
    handler is hooked up. This applies only to the main application
    thread. The UnhandledException event is raised for unhandled
    exceptions thrown in other threads</p>
  </blockquote>
  
  <p>So, the resulting code will look like the following:</p>
  
  <pre><code>    public static void UnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs e)
      {
          // ...
      }
  
      [STAThread]
      static void Main(string[] args)
      {
          Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
          AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionEventHandler);
  
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new MainForm(pathToCheck));
      }
  </code></pre>
  
  <p>Is it ok? Will it catch all unhandled exceptions from any threads (including the main thread, UI thread and all threads created by <code>Task</code> class)? Did I understand the documentation correctly?</p>
  
  <p>Yes, I saw questions like <a href="https://stackoverflow.com/questions/5762526/how-can-i-make-something-that-catches-all-unhandled-exceptions-in-a-winforms-a">this</a> here, but I don't understand why should I also use the following code:</p>
  
  <pre><code>Application.ThreadException += new     
    ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);
  </code></pre>