wpf当中 捕捉全局异常

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
DispatcherUnhandledException += App_DispatcherUnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
 private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs ex)
 {
     //MessageBox.Show("Task线程异常:" + ex.me, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
     设置该异常已察觉(这样处理后就不会引起程序崩溃)
     ex.SetObserved();


 }

 private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
 {
     e.Handled = true;
     MessageBox.Show("发生错误:" + e.Exception.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);

 }

 private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
 {
     Exception ex = (Exception)e.ExceptionObject;
     MessageBox.Show("发生错误:" + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
 }

你可能感兴趣的:(wpf)