when you use C# to build Windows Forms Application you come to a point when you need a general Exception handing for both you UI and any thread in the application ... its easy just add the following to your Main method in the Program class
and make sure you add the following methods to the Program Class
// Add the event handler for handling UI thread exceptions to the event. Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException); // Set the unhandled exception mode to force all Windows Forms errors to go through // our handler. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); // Add the event handler for handling non-UI thread exceptions to the event. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException);
and make sure you add the following methods to the Program Class
private static void UIThreadException(object sender, ThreadExceptionEventArgs t) { // Handel your Exception here MessageBox.Show(t.Exception.Message, "error"); } private static void UnhandledException(object sender, UnhandledExceptionEventArgs e) { // Handel your Exception here var ex = e.ExceptionObject as Exception; MessageBox.Show(ex.Message, "error"); }