C # captures global exceptions

Source: Internet
Author: User

Using System; Using System.Collections.Generic; Using System.Windows.Forms; Using System.IO; namespace Gobalexception {    static class program     {     & nbsp; //<summary>         Main entry point for the application.        //</summary>         [ STAThread]         static void Main ()         {            try              {                //Handling uncaught exceptions                     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 );

                Application.enablevisualstyles ();                 Application.setcompatibletextrenderingdefault (FALSE);                 Application.Run (new Form1 ());            }              catch (Exception ex)             {                 string str = "";                 string strdateinfo = " An unhandled exception occurred for 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 ("shouldApplication Thread error: {0} ", ex);                }

Writelog (str); 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 handle the unhandled exception, I write the error details to the text, such as the error pop up a beautiful error prompt form, to give you a reference        ///practice a lot, It can be to record error details to text, database, send error message to author mailbox or re-initialize after error, etc.        ////This is the benevolent see of the beholder, we have done it ourselves.        //</summary>        //< param name= "Sender" ></param>        //<param name= "E" ></ param>         static void Application_threadexception (object sender, System.Threading.ThreadExceptionEventArgs e)         {                         String str = "";             string strdateinfo = "The application process appearsSequence Unhandled Exception: "+ 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);                 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);            }        & NBsp;    Else             {                 str = string. Format ("Application unhandlederror:{0}", E);            }

            Writelog (str);             MessageBox.Show ("Fatal error occurred, please stop the current operation and contact the author in time!") "," System error ", MessageBoxButtons.OK, Messageboxicon.error);        }        //<summary>         //write files        //</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 # captures global exceptions

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.