Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Last active December 26, 2015 15:19
Show Gist options
  • Save yemrekeskin/7171725 to your computer and use it in GitHub Desktop.
Save yemrekeskin/7171725 to your computer and use it in GitHub Desktop.
UnhandledException
// in console application
class Program
{
static void Main(string[] args)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += currentDomain_UnhandledException;
Console.ReadLine();
}
static void currentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = (Exception)e.ExceptionObject;
// logging and warning messages
Console.WriteLine("MyHandler caught : " + ex.Message);
Console.WriteLine("Runtime terminating: {0}", e.IsTerminating);
}
}
// in windows form application
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.ThreadException += Application_ThreadException;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
Exception ex = e.Exception;
MessageBox.Show(ex.Message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment