WinForm Catching global exceptions

Source: Internet
Author: User

<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Try
{
Set Application handling Exception mode: ThreadException processing
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 );

#region The main entry point of the application
Application.enablevisualstyles ();
Application.setcompatibletextrenderingdefault (FALSE);
Application.Run (New Formlogin ());

#endregion
}
catch (Exception ex)
{
String str = Getexceptionmsg (ex, String. Empty);
MessageBox.Show (str, "System error", MessageBoxButtons.OK, Messageboxicon.error);
System.Environment.Exit (0);
Application.restart ();

}
}

static void Application_threadexception (object sender, System.Threading.ThreadExceptionEventArgs e)
{
String str = getexceptionmsg (e.exception, e.tostring ());
MessageBox.Show (str, "System error", MessageBoxButtons.OK, Messageboxicon.error);
System.Environment.Exit (0);
Application.restart ();


}

static void Currentdomain_unhandledexception (object sender, UnhandledExceptionEventArgs e)
{
String str = getexceptionmsg (e.exceptionobject as Exception, e.tostring ());
MessageBox.Show (str, "System error", MessageBoxButtons.OK, Messageboxicon.error);
System.Environment.Exit (0);
Application.restart ();
}

<summary>
To generate a custom exception message
</summary>
<param name= "Ex" > Exception objects </param>
<param name= "Backstr" > Standby Exception message: Valid when EX is null </param>
<returns> Exception String Text </returns>
static string Getexceptionmsg (Exception ex,string backstr)
{
StringBuilder sb = new StringBuilder ();
Sb. Appendline ("**************************** exception text ****************************");
Sb. Appendline ("Occurrence Time": "+ DateTime.Now.ToString ()");
if (ex! = null)
{
Sb. Appendline ("" Exception type: "+ ex.) GetType (). Name);
Sb. Appendline ("Exception info": "+ Ex.") Message);
Sb. Appendline ("" Stack call ":" + ex.) StackTrace);
}
Else
{
Sb. Appendline ("Unhandled Exception": "+ backstr);
}
Sb. Appendline ("***************************************************************");
Return SB. ToString ();
System.Environment.Exit (0);
Application.restart ();
}

WinForm Catching global exceptions

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.