Close application without .net framework's error prompt window

前端 未结 2 1865
情书的邮戳
情书的邮戳 2021-01-27 14:12

Codes handles unhandled exceptions as below in my project.

   static void FnUnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs _UnhandledE         


        
2条回答
  •  抹茶落季
    2021-01-27 14:34

    You should be able to catch everything with this

        [STAThread]
        public static void Main()
        {
            // let IDE to handle exceptions
            if (System.Diagnostics.Debugger.IsAttached)
                Run();
            else
                try
                {
                    Application.ThreadException += Application_ThreadException;
                    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                    AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                    Run();
                }
                catch (Exception e)
                {
                    // catch exceptions outside of Application.Run
                    UnhandledException(e);
                }
        }
    
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            // catch non-ui exceptions
            UnhandledException(e.ExceptionObject as Exception);
        }
    
        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            // catch ui exceptions
            UnhandledException(e.Exception);
        }
    
        private static void UnhandledException(Exception e)
        {
            try
            {
                // here we restart app
            }
            catch
            {
                // if we are here - things are really really bad
            }
        }
    

提交回复
热议问题