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