C # WinForm capture unprocessed exceptions,

Source: Internet
Author: User

C # WinForm capture unprocessed exceptions,

Using System; using System. collections. generic; using System. windows. forms; using System. IO; namespace GobalException {static class Program {/// <summary> /// main entry point of the application. /// </Summary> [STAThread] static void Main () {try {// process uncaptured exception Application. setUnhandledExceptionMode (UnhandledExceptionMode. catchException); // handle the UI thread exception Application. threadException + = new System. threading. threadExceptionEventHandler (Application_ThreadException); // handle non-UI thread exception AppDomain. currentDomain. unhandledException + = new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException); Applicat Ion. enableVisualStyles (); Application. setCompatibleTextRenderingDefault (false); Application. run (new Form1 ();} catch (Exception ex) {string str = ""; string strDateInfo = "Exceptions not handled by the application:" + DateTime. now. toString () + "\ r \ n"; if (ex! = Null) {str = string. format (strDateInfo + "exception type: {0} \ r \ n exception message: {1} \ r \ n exception message: {2} \ r \ n", ex. getType (). name, ex. message, ex. stackTrace);} else {str = string. format ("Application Thread error: {0}", ex) ;}writelog (str); // frmBug f = new frmBug (str ); // friendly prompt interface // f. showDialog (); MessageBox. show ("a fatal error occurs. Please contact the author in time! "," System error ", MessageBoxButtons. OK, MessageBoxIcon. error); }}/// <summary> /// this is the method we want to handle when an unhandled exception occurs. I am writing the Error details to the text, if an error occurs, a beautiful error prompt form is displayed. There are many practices for reference. You can record the error details to the text and database, send an error email to the author's mailbox or re-initialize the email after an error occurs. // This is what the benevolent sees the wise, and you have done it yourself. /// </Summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> static void Application_ThreadException (object sender, system. threading. threadExceptionEventArgs e) {string str = ""; string strDateInfo = "Exceptions not handled by the application:" + 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 message: {2} \ r \ n", error. getType (). name, error. message, error. stackTrace);} else {str = string. format ("Application Thread error: {0}", e) ;}writelog (str); // frmBug f = new frmBug (str ); // friendly prompt interface // f. showDialog (); MessageBox. show ("a fatal error occurs. Please contact the author in time! "," System error ", MessageBoxButtons. OK, MessageBoxIcon. error);} static void CurrentDomain_UnhandledException (object sender, UnhandledExceptionEventArgs e) {string str = ""; Exception error = e. exceptionObject as Exception; string strDateInfo = "Exceptions not handled by the application:" + DateTime. now. toString () + "\ r \ n"; if (error! = Null) {str = string. format (strDateInfo + "Application UnhandledException: {0}; \ n \ r stack information: {1}", error. message, error. stackTrace);} else {str = string. format ("Application UnhandledError: {0}", e) ;}writelog (str); // frmBug f = new frmBug (str); // friendly prompt interface // f. showDialog (); MessageBox. show ("a fatal error occurs. Please stop the current operation and contact the author in time! "," System error ", MessageBoxButtons. OK, MessageBoxIcon. error );} /// <summary> /// write a file /// </summary> /// <param name = "str"> </param> static void writeLog (string str) {if (! Directory. exists ("ErrLog") {Directory. createDirectory ("ErrLog");} using (StreamWriter sw = new StreamWriter (@ "ErrLog \ ErrLog.txt", true) {sw. writeLine (str); sw. writeLine ("---------------------------------------------------------"); sw. close ();}}}}

 


C Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

C Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

Related Article

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.