c# 異常控制代碼

來源:互聯網
上載者:User

c# 異常控制代碼 好象以前寫過,再寫一次.

 

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Windows.Forms;
using System.Threading;

namespace ttt
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // Add the event handler for handling UI thread exceptions to the event.     
            Application.ThreadException += new ThreadExceptionEventHandler(Form_UIThreadException);

            // Set the unhandled exception mode to force all Windows Forms errors to go through     
            // our handler.     
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            // Add the event handler for handling non-UI thread exceptions to the event.      
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);   

            //--------------------------------------------------

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form_main());
        }//

        // Handle the UI exceptions by showing a dialog box, and asking the user whether     
        // or not they wish to abort execution.     
        private static void Form_UIThreadException(object sender, ThreadExceptionEventArgs t)
        {
            MessageBox.Show("異常控制代碼 1:" + t.Exception.ToString());
        }//

        // Handle the UI exceptions by showing a dialog box, and asking the user whether     
        // or not they wish to abort execution.     
        // NOTE: This exception cannot be kept from terminating the application - it can only      
        // log the event, and inform the user about it.  
        //這個是沒法阻止程式退出的,只能做記錄
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            //e.IsTerminating;//這個屬性告訴我們這個異常是否會導致應用終止

            Exception ex = (Exception)e.ExceptionObject;
            string errorMsg = "An application error occurred. Please contact the adminstrator " +
            "with the following information:\n\n";
            errorMsg += ex.Message + "\n\nStack Trace:\n" + ex.StackTrace;

            MessageBox.Show("異常控制代碼 2:" + errorMsg);
        }//

    }//
}//

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.