WPF finishing chapter to deal with the global exception handler

Original: WPF finishing chapter to deal with the global exception handler

When we write the code to the code error exception handling sometimes will not do deal 

For example, we execute the following code will cause the program to crash


  
  
  1. private void Button_Click(object sender, RoutedEventArgs e)
  2. {
  3. throw new Exception( "字符串引发异常 日志输出");
  4. }

This is unacceptable to us 

 

When we encounter a problem to find one to fear that there is no one way to intercept all exceptions that otherwise hang up program?

Some WPF in dispatcherUnhandledException

 

We rewrite Onstartup method in APP


  
  
  1. public partial class App : Application
  2. {
  3. protected override void OnStartup(StartupEventArgs e)
  4. {
  5. base.OnStartup(e);
  6. DispatcherUnhandledException += App_DispatcherUnhandledException;
  7. }
  8. void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  9. {
  10. MessageBox.Show(e.Exception.ToString());
  11. e.Handled = true;
  12. // throw new NotImplementedException();
  13. }
  14. }

Here we can intercept all unhandled exceptions can write log or reboot

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12075432.html