Monday, January 28, 2019

C#: Handle unhandled exceptions

namespace Vurdalakov
{
    using System;
    using System.Windows.Forms;

    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
            AppDomain.CurrentDomain.UnhandledException += (s, e) => HandleUnhandledException(e.ExceptionObject as Exception);

            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                Application.Run(new MainForm());
            }
            catch (Exception ex)
            {
                HandleUnhandledException(ex);
            }

            Environment.Exit(0);
        }

        private static void HandleUnhandledException(Exception ex)
        {
            // Handle unhandled exception

            Application.Exit();
        }
    }
}

No comments:

Post a Comment