C # winform capture global exception

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

GobalException namespace
{
    static class Program
    {
        /// <Summary>
        main entry point /// application.
        /// </ Summary>
        [STAThread]
        static void the Main ()
        {
            the try
            {
                // uncaught exception processing  
                Application.SetUnhandledExceptionMode (UnhandledExceptionMode.CatchException);
                // process the UI thread exception  
                Application.ThreadException + = new System.Threading. ThreadExceptionEventHandler (Application_ThreadException);
                // handle non-UI thread exception  
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

                Application.EnableVisualStyles ();
                Application.SetCompatibleTextRenderingDefault (to false);
                the Application.Run (new new the Form1 ());
            }
            the catch (Exception EX)
            {
                String STR = "";
                String = strDateInfo "appear in the application unhandled exception:" + 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 & lt \ n-",
                         EX . .GetType () the Name, ex.Message, ex.StackTrace);
                }
                the else
                {
                    STR = string.Format ( "application threads error: {0}", EX);
                }


                writeLog (str);
                MessageBox.Show ( "Fatal error, please contact the author!", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        /// <the Summary>
        /// This method is what we want to exception handling in untreated occurred to me it is wrong to write detailed information to the text, such as error after error message pops up a beautiful form, to be a reference
        /// practice a lot, the record may be wrong details to the text, database, send an error message to the mailbox of an error or re-initialization, etc.
        /// this is the eyes of the beholder wise see wisdom, we all do.
        /// </ Summary>
        /// <param name = "SENDER"> </ param>
        /// <param name = "E"> </ param>
        static void Application_ThreadException (Object SENDER, System.Threading.ThreadExceptionEventArgs E )
        {
           
            String STR = "";
            String = strDateInfo "application unhandled exception occurs:" DateTime.Now.ToString + () + "\ R & lt \ n-";
            exception error exception AS = e.Exception;
            IF (error!

                str = string.Format (strDateInfo + "Exception Type: {0} \ r \ n exception message: {1} \ r \ n exception information: {2} \ R & lt \ n-",
                     . error.GetType () the Name, error .Message, error.StackTrace);
            }
            the else
            {
                STR = string.Format ( "application threads error: {0}", E);
            }

            writeLog (str);   
            MessageBox.Show ( "Fatal error, please contact the author!", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            string str = "";
            Exception error = e.ExceptionObject as Exception;
            string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
            if (error != null)
            {
                str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆栈信息:{1}", error.Message, error.StackTrace);
            }
            else
            {
                str = string.Format("Application UnhandledError:{0}", e);
            }

            writeLog (str);
            MessageBox.Show ( "Fatal error, please stop the current operation and promptly contact the author!", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        /// <the Summary>
        /// write 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();
            }
        }
    }
}

Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2011/11/02/2233420.html

Guess you like

Origin blog.csdn.net/weixin_33795806/article/details/93361699