How to catch the WinForm program exception, of course, is from the program start the entry of the programs class is defined by the main () method, see below How to write this class?
Static classProgram {Static stringRunformfullname {Get { stringSetrunformfullname =Cipace. Sys.Configuration.RunFormFullName; if(Setrunformfullname = =NULL) Setrunformfullname=Digiform.setrunformfullname; returnSetrunformfullname; } } /// <summary> ///The main entry point for the application. /// </summary> Public StaticApplicationContext context; [STAThread]Private Static voidMain () {Try { //Handling uncaught ExceptionsApplication.setunhandledexceptionmode (unhandledexceptionmode.catchexception); //Handling UI Thread exceptionsApplication.ThreadException + =application_threadexception; //handling non-UI thread exceptionsAppDomain.CurrentDomain.UnhandledException + =currentdomain_unhandledexception; varAprocessname =process.getcurrentprocess (). ProcessName; if((Process.getprocessesbyname (Aprocessname)). GetUpperBound (0) >0) {MessageBox.Show (@"The system is already running, if you want to restart, close from the process ...",@"System Warning", MessageBoxButtons.OK, Messageboxicon.asterisk); } Else{application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); Context=NewApplicationContext (); Application.idle+ = Application_idle;//The registration program runs idle to execute the main program form corresponding initialization codeApplication.Run (context); } } Catch(Exception ex) {LogNet.Log.WriteLog ("Main", ex); MessageBox.Show ("An exception occurred on the system:"+ (ex. Message +" "+ (ex. InnerException! =NULL&& Ex. Innerexception.message! =NULL&& Ex. Message! = Ex. Innerexception.message? Ex. Innerexception.message:""))+", please restart the program. "); Digiform Digiform=NewDigiform (); Digiform.updateappsettings (Digiform.runformfullname, digiform.loginformfullname); } } Private Static voidApplication_idle (Objectsender, EventArgs e) {Application.idle-=Application_idle; if(Context. MainForm = =NULL) {Form form=new LoginForm (); Context. MainForm=form; Form. Show (); } } Private Static voidApplication_threadexception (Objectsender, Threadexceptioneventargs e) { varex =e.exception; if(Ex! =NULL) {LogNet.Log.WriteLog ("application_threadexception", ex); } MessageBox.Show ("An exception occurred on the system:"+ (ex. Message +" "+ (ex. InnerException! =NULL&& Ex. Innerexception.message! =NULL&& Ex. Message! = Ex. Innerexception.message? Ex. Innerexception.message:""))); } Private Static voidCurrentdomain_unhandledexception (Objectsender, UnhandledExceptionEventArgs e) { varex = E.exceptionobject asException; if(Ex! =NULL) {LogNet.Log.WriteLog ("currentdomain_unhandledexception", ex); } MessageBox.Show ("An exception occurred on the system:"+ (ex. Message +" "+ (ex. InnerException! =NULL&& Ex. Innerexception.message! =NULL&& Ex. Message! = Ex. Innerexception.message? Ex. Innerexception.message:""))); } }
WinForm program Global catch Exception handling method