C # WinForm Capture global variable exception Samwang solution _c# Tutorial

Source: Internet
Author: User
Tags datetime exception handling static class
Many small company projects are missing the exception handling module, we are also. Often this happens, the user in the UI interface operation, directly jump out of the stack call of the Exception Information dialog box, the boss saw that call a fire Ah! How does your code appear garbled every day? Oh! This is the result of no exception capture processing, and now many people write code is not aware of handling exceptions, as long as the implementation of the function is good, many of my team members are the same.

Project has just taken over, so I intend to do an exception global capture, unified processing mode, the use of specific details of the dialog box to remind and log file save way. The following is based on the Internet to find the C#winform global exception capture made a little change. (such as project exception handling all completed, the experience to make a record, where the global exception capture to make a record)
Copy Code code as follows:

Static Class Program
{
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Try
{
To set the 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 an application
Application.enablevisualstyles ();
Application.setcompatibletextrenderingdefault (FALSE);
Application.Run (New Form1 ());
#endregion
}
catch (Exception ex)
{
String str = getexceptionmsg (ex,string. Empty);
MessageBox.Show (str, "System error", MessageBoxButtons.OK, Messageboxicon.error);
}
}


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);
Logmanager.writelog (str);
}

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);
Logmanager.writelog (str);
}

<summary>
Generating custom Exception messages
</summary>
<param name= "Ex" > Exception object </param>
<param name= "Backstr" > Alternate 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 ();
}
}

Reference:
Copy Code code as follows:

Static Class Program
{
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Try
{
Handling exceptions that are not caught
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 an application

Application.enablevisualstyles ();
Application.setcompatibletextrenderingdefault (FALSE);
Application.Run (New Main ());

#endregion

}
catch (Exception ex)
{
String str = "";
String Strdateinfo = "Exception occurred for application unhandled:" + 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 ("Application thread error: {0}", ex);
}

MessageBox.Show (str, "System error", MessageBoxButtons.OK, Messageboxicon.error);
Logmanager.writelog (str);
}

}

static void Application_threadexception (object sender, System.Threading.ThreadExceptionEventArgs e)
{
String str = "";
String Strdateinfo = "Exception occurred for application unhandled:" + 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);
}

MessageBox.Show (str, "System error", MessageBoxButtons.OK, Messageboxicon.error);
Logmanager.writelog (str);
}

static void Currentdomain_unhandledexception (object sender, UnhandledExceptionEventArgs e)
{
String str = "";
Exception error = E.exceptionobject as Exception;
String Strdateinfo = "Exception occurred for application unhandled:" + DateTime.Now.ToString () + "\ r \ n";
if (Error!= null)
{
str = string. Format (Strdateinfo + "Application unhandledexception:{0};\n\r Stack info: {1}", error. Message, error. StackTrace);
}
Else
{
str = string. Format ("Application unhandlederror:{0}", E);
}

MessageBox.Show (str, "System error", MessageBoxButtons.OK, Messageboxicon.error);
Logmanager.writelog (str);
}
}

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.