C # WinForm captures unhandled exception instance resolution

Source: Internet
Author: User

This article is a complete example of how C # WinForm captures unhandled exceptions. Share it for everyone's reference. The specific code is as follows:

Using system;using system.collections.generic;using system.windows.forms;using system.io;namespace GobalException{    Static class Program {///<summary>//The main entry point of the application. </summary> [STAThread] static void Main () {try {//handle uncaught exception APPLICATION.SETUNH        Andledexceptionmode (unhandledexceptionmode.catchexception); Handling UI Thread Exceptions Application.ThreadException + = new System.Threading.ThreadExceptionEventHandler (application_threadexcep        tion); Handling non-UI thread exceptions AppDomain.CurrentDomain.UnhandledException + = new Unhandledexceptioneventhandler (Currentdomain_unhandl        Edexception);        Application.enablevisualstyles ();        Application.setcompatibletextrenderingdefault (FALSE);      Application.Run (New Form1 ());        } catch (Exception ex) {string str = "";        String strdateinfo = "An unhandled exception occurred 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 information: {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 ("Fatal error occurred, please contact the author in time!")      "," System error ", MessageBoxButtons.OK, Messageboxicon.error); }}////<summary>////This is the way we are going to deal with an unhandled exception, I write the error details to the text, such as an error pop up a beautiful error prompt form, give you a reference///practice a lot, can be the error details recorded in the text    This, database, send error message to author mailbox or re-initialize after error, etc.///This is the benevolent see of the beholder, we did it ourselves. </summary>//<param name= "sender" ></param>//<param name= "E" ></param> Stati      c void Application_threadexception (object sender, System.Threading.ThreadExceptionEventArgs e) {string str = "";      String strdateinfo = "An unhandled exception occurred 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 information: {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 ("Fatal error occurred, 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 = "An unhandled exception occurred 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 ("Fatal error occurred, please stop the current operation and contact the author in time!") "," System error ", MeSsageboxbuttons.ok, Messageboxicon.error); }///<summary>///////</summary>//<param name= "str" ></param> static void W Ritelog (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 (); }    }  }}

The examples in this paper are provided with more detailed comments to facilitate reading comprehension. I hope this article is helpful to everyone's C # programming.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # WinForm captures unhandled exception instance resolution

This address: http://www.paobuke.com/develop/c-develop/pbk23535.html






Related content examples of simple usages of sharpmap in C # operation SQLite database Help Class A summary of how a DataTable is converted to JSON in C # (three ways) C # How to eliminate the jagged effect of captcha pictures
On the conversion of several formats of C # serialization and deserialization C # simple implementation of ways to prevent multiple programs from running in C # winfrom default Input Method setting in C # Common naming rules Rollup

C # WinForm captures unhandled exception instance resolution

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.