C# Winform捕捉全局异常


/// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false);   Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //处理非UI线程 AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; //处理UI线程 Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);   Application.Run(new Form1());   glExitApp = true; } /// /// 异常处理 /// static bool glExitApp = false;//是否退出 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine("not ui excettion is "); while (true) { if (glExitApp) { return; } Thread.Sleep(2000); } } static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { Console.WriteLine("ui excettion is " + e.Exception.Message); }