General Windows Application Exception Handling C#
June 15, 2013 at 3:16 AM
—
admin
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
// 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
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");
90deac79-417e-40d7-94e6-3e52af1dfe14|0|.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04
Posted in: .NET | IT
Tags: