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

原文地址:https://www.cnblogs.com/yaosj/p/10083566.html