1 Static class Program2 {3 /// <summary>4 ///The main entry point for the application. 5 /// </summary>6 [STAThread]7 Static voidMain ()8 {9 TryTen { One A //add an exception that is not caught by the event handler - Application.setunhandledexceptionmode (unhandledexceptionmode.catchexception); - //add event handling UI thread exception theApplication.ThreadException + =NewSystem.Threading.ThreadExceptionEventHandler (application_threadexception); - //Add event handling non-UI thread exception -AppDomain.CurrentDomain.UnhandledException + =NewUnhandledexceptioneventhandler (currentdomain_unhandledexception); - + - application.enablevisualstyles (); +Application.setcompatibletextrenderingdefault (false); AApplication.Run (Newfrmactivity ()); at } - Catch(Exception ex) - { - stringstr =""; - stringStrdateinfo ="An unhandled exception occurred with the application:"+ DateTime.Now.ToString () +"\ r \ n"; - in if(Ex! =NULL) - { tostr =string. Format (Strdateinfo +"Exception Type: {0}\r\n exception message: {1}\r\n exception information: {2}\r\n", + Ex. GetType (). Name, ex. Message, ex. StackTrace); - } the Else * { $str =string. Format ("Application Thread error: {0}", ex);Panax Notoginseng } - the //Write Log + Writelog.writeerrlog (str); AMessageBox.Show ("A fatal error occurred, please contact the author in time! ","System Error", MessageBoxButtons.OK, messageboxicon.error); the } + - } $ $ /// <summary> - ///This is how we deal with unhandled exceptions, many of which can be done by logging the error details to the text, the database, sending an error message to the author's mailbox or re-initializing it after an error, and so on. - /// </summary> the /// <param name= "Sender" ></param> - /// <param name= "E" ></param>Wuyi Static voidApplication_threadexception (Objectsender, System.Threading.ThreadExceptionEventArgs e) the { - Wu stringstr =""; - stringStrdateinfo ="An unhandled exception occurred with the application:"+ DateTime.Now.ToString () +"\ r \ n"; AboutException error = E.exception asException; $ if(Error! =NULL) - { -str =string. Format (Strdateinfo +"Exception Type: {0}\r\n exception message: {1}\r\n exception information: {2}\r\n", - error. GetType (). Name, error. Message, error. StackTrace); A } + Else the { -str =string. Format ("Application Thread error: {0}", e); $ } the //Write Log the Writelog.writeerrlog (str); theMessageBox.Show ("A fatal error occurred, please contact the author in time! ","System Error", MessageBoxButtons.OK, messageboxicon.error); the } - /// <summary> in ///' Handling UI exceptions the /// </summary> the /// <param name= "Sender" ></param> About /// <param name= "E" ></param> the Static voidCurrentdomain_unhandledexception (Objectsender, UnhandledExceptionEventArgs e) the { the stringstr =""; +Exception error = E.exceptionobject asException; - stringStrdateinfo ="An unhandled exception occurred with the application:"+ DateTime.Now.ToString () +"\ r \ n"; the if(Error! =NULL)Bayi { thestr =string. Format (Strdateinfo +"Application unhandledexception:{0};\n\r Stack information: {1}", error. Message, error. StackTrace); the } - Else - { thestr =string. Format ("Application Unhandlederror:{0}", e); the } the //Write Log the Writelog.writeerrlog (str); -MessageBox.Show ("A fatal error occurred, please stop the current operation and contact the author in time! ","System Error", MessageBoxButtons.OK, messageboxicon.error); the } the the}
WinForm C # Global Error capture Processing "grooming"