I\'m currently in the process of writing my first Windows Forms application. I\'ve read a few C# books now so I\'ve got a relatively good understanding of what language feat
you can trap the ThreadException event.
Select a Windows Application project in Solution Explorer.
Open the generated Program.cs file by double-clicking on it.
Add the following line of code to the top of the code file:
using System.Threading;
In the Main() method, add the following as the first line of the method:
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
Add the following below the Main() method:
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
// Do logging or whatever here
Application.Exit();
}
Add code to handle the unhandled exception within the event handler. Any exception that is not handled anywhere else in the application is handled by the above code. Most commonly, this code should log the error and display a message to the user.
refrence: https://blogs.msmvps.com/deborahk/global-exception-handler-winforms/