C#未捕获异常处理方法

来源:互联网 发布:java 引号处理 编辑:程序博客网 时间:2024/06/14 20:04

C#经常会因为未捕获的异常而造成应用程序崩溃,以下是解决方法:

1.WPF捕获

            Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);        void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)        {            MessageBox.Show(e.Exception.Message + "\r\n" + e.Exception.StackTrace, "系统信息");        }


2.winform捕获

            System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);            System.Windows.Forms.Application.SetUnhandledExceptionMode(System.Windows.Forms.UnhandledExceptionMode.CatchException);            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)        {            MessageBox.Show(((Exception)e.ExceptionObject).Message + "\r\n" + ((Exception)e.ExceptionObject).StackTrace, "系统信息");        }        void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)        {            MessageBox.Show(e.Exception.Message + "\r\n" + e.Exception.StackTrace, "系统信息");        }


原创粉丝点击