WinForm program Global catch Exception handling method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.