c#處理未捕獲的異常(UnhandledException)


處理未捕獲的異常,放在program類的Main函數下

1.UnhandledException

作用:接收未捕獲到的異常

例:

        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        }
 
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Console.WriteLine(e.ExceptionObject.ToString());
        }

2.ThreadException

作用:winform接收UI線程的異常

        static void Main()
        {Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
            Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException);
        }
 
        private static void UIThreadException(object sender, ThreadExceptionEventArgs t)
        {
        }

  

   參考:https://blog.csdn.net/lrh_079/article/details/7265486


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM