<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