處理Application.ThreadException異常, 攔截GUI主線程的異常

來源:互聯網
上載者:User

標籤:winform   style   color   io   os   ar   for   div   sp   

 .Net Winform有著自己的未處理異常機制。 Winform內部的代碼會在Applicatin.Run方法裡面為訊息迴圈建立一個大的try...catch。 這樣, 任何在GUI主線程裡面的異常都會被這個try...catch所捕捉到, 這個預設的未處理異常handler會提取異常的所有資訊然後顯示在一個錯誤對話方塊裡面,接著程式就中止了。這樣我的try...catch就沒有作用了,即下面的捕捉異常的代碼是無效的。static void Main()
{
    try
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new mainfrm());
    }
     catch(Exception e)
    {
        MessgaeBox.Show(e.Message);
    }
}
 對於這個問題,可以處理Application.ThreadException異常,這樣就可以攔截GUI主線程裡面的異常了:

        static void Main()
        {
            //// Setup unhandled exception handlers
            //AppDomain.CurrentDomain.UnhandledException += // CLR
            //   new UnhandledExceptionEventHandler(OnUnhandledException);

 

            Application.ThreadException += // Windows Forms
         new System.Threading.ThreadExceptionEventHandler(
             OnGuiUnhandedException);


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

 

        //// CLR unhandled exception
        //private static void OnUnhandledException(Object sender,
        //   UnhandledExceptionEventArgs e)
        //{
        //    HandleUnhandledException(e.ExceptionObject);
        //}

 

        // Windows Forms unhandled exception
        private static void OnGuiUnhandedException(Object sender,
           ThreadExceptionEventArgs e)
        {
            HandleUnhandledException(e.Exception);
        }

 

        static void HandleUnhandledException(Object o)
        {
            Exception e = o as Exception;

            if (e != null)
            { // Report System.Exception info
                MessageBox.Show("Exception = " + e.GetType());
                MessageBox.Show("Message = " + e.Message);
                MessageBox.Show("FullText = " + e.ToString());
            }
            else
            { // Report exception Object info
                MessageBox.Show("Exception = " + o.GetType());
                MessageBox.Show("FullText = " + o.ToString());
            }

            MessageBox.Show("An unhandled exception occurred " +
               "and the application is shutting down.");
            Environment.Exit(1); // Shutting down
        }

 

上述狀態是編譯成EXE後調試的結果,在IDE環境中情況不同。

處理Application.ThreadException異常, 攔截GUI主線程的異常

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.