Using System; Using System.Collections.Generic; Using System.Windows.Forms; Using System.IO; namespace Gobalexception { static class program { & nbsp; //<summary> Main entry point for the application. //</summary> [ STAThread] static void Main () { try { //Handling uncaught exceptions Application.setunhandledexceptionmode (unhandledexceptionmode.catchexception); //Handling UI thread exceptions Application.ThreadException + = new System.Threading.ThreadExceptionEventHandler (application_threadexception); //handling non-UI thread exceptions AppDomain.CurrentDomain.UnhandledException + = new Unhandledexceptioneventhandler (currentdomain_unhandledexception );
Application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (FALSE); Application.Run (new Form1 ()); } catch (Exception ex) { string str = ""; string strdateinfo = " An unhandled exception occurred for the application: "+ DateTime.Now.ToString () +" \ r \ n ";
if (ex! = null) { str = 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); } Else { str = string. Format ("shouldApplication Thread error: {0} ", ex); }
Writelog (str); MessageBox.Show ("Fatal error occurred, please contact the author in time!") "," System error ", MessageBoxButtons.OK, Messageboxicon.error); }
}
//<summary> // This is the way we are going to handle the unhandled exception, I write the error details to the text, such as the error pop up a beautiful error prompt form, to give you a reference ///practice a lot, It can be to record error details to text, database, send error message to author mailbox or re-initialize after error, etc. ////This is the benevolent see of the beholder, we have done it ourselves. //</summary> //< param name= "Sender" ></param> //<param name= "E" ></ param> static void Application_threadexception (object sender, System.Threading.ThreadExceptionEventArgs e) { String str = ""; string strdateinfo = "The application process appearsSequence Unhandled Exception: "+ DateTime.Now.ToString () +" \ r \ n "; Exception error = e.exception as Exception; 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); } Else { str = string. Format ("Application thread error: {0}", e); }
writelog (str); MessageBox.Show ("Fatal error occurred, please contact the author in time! "," System error ", MessageBoxButtons.OK, Messageboxicon.error); }
static void Currentdomain_unhandledexception (object sender, UnhandledExceptionEventArgs e) { string str = ""; Exception error = E.exceptionobject as Exception; string strdateinfo = "An unhandled exception occurred by the application:" + DateTime.Now.ToString () + "\ r \ n"; if (Error! = NULL) { str = string. Format (Strdateinfo + "Application unhandledexception:{0};\n\r stack information: {1}", error. Message, error. StackTrace); } & NBsp; Else { str = string. Format ("Application unhandlederror:{0}", E); }
Writelog (str); MessageBox.Show ("Fatal error occurred, please stop the current operation and contact the author in time!") "," System error ", MessageBoxButtons.OK, Messageboxicon.error); } //<summary> //write files //</summary> //<param name= "str" ></param> static void Writelog (String str) { if (! Directory.Exists ("ErrLog")) { Directory.CreateDirectory (" ErrLog "); }
using (StreamWriter SW = new StreamWriter (@ "Errlog\errlog.txt", True)) {SW. WriteLine (str); Sw. WriteLine ("---------------------------------------------------------"); Sw. Close (); } } } }
C # captures global exceptions