WinForms Global Exception Handling?

后端 未结 7 1309
甜味超标
甜味超标 2020-11-27 05:17

I have implemented a software which have a DLL library which contains a huge set of classes which includes all the methods for my software.

Now i want to be able to

7条回答
  •  死守一世寂寞
    2020-11-27 05:49

    Global error interception in winforms

        static class Program
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
                Application.Run(new myForm());
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
    
        internal static void HandleException(Exception ex)
        {
            string LF = Environment.NewLine + Environment.NewLine;
            string title = $"Oups... I got a crash at {DateTime.Now}";            
            string infos = $"Please take a screenshot of this message\n\r\n\r" +
                           $"Message : {LF}{ex.Message}{LF}" +
                           $"Source : {LF}{ex.Source}{LF}" +
                           $"Stack : {LF}{ex.StackTrace}{LF}" +
                           $"InnerException : {ex.InnerException}";
    
            MessageBox.Show(infos, title, MessageBoxButtons.OK, MessageBoxIcon.Error); // Do logging of exception details
        }
    }
    

提交回复
热议问题