简体   繁体   English

WPF如何处理异常并继续

[英]WPF how to handle Exceptions and continue

I originally had code to handle DispatcherUnhandledException which would log the error and mark the exception as handled 我最初有代码来处理DispatcherUnhandledException ,它会记录错误并将异常标记为已处理

protected override void OnStartup(StartupEventArgs e)
{
    Dispatcher.UnhandledException += OnDispatcherUnhandledException;
}
...
void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
    // Log Error here
    e.Handled = true;
}

I've tried to improve this by covering a wider range of unhandled exceptions 我试图通过涵盖更广泛的未处理异常来改善这一点

    protected override void OnStartup(StartupEventArgs e)
    {
         AppDomain.CurrentDomain.UnhandledException += (s, ex) =>
         LogUnhandledException((Exception)ex.ExceptionObject, 
        "AppDomain.CurrentDomain.UnhandledException");

         DispatcherUnhandledException += (s, ex) =>
         LogUnhandledException(ex.Exception, 
         "Application.Current.DispatcherUnhandledException");

         TaskScheduler.UnobservedTaskException += (s, ex) =>
         LogUnhandledException(ex.Exception, 
         "TaskScheduler.UnobservedTaskException");
    }

but I cannot handle the exceptions using this event 但我无法使用此事件处理异常

    private void LogUnhandledException(Exception e, string @event)
    {
      // Log Error here
      e.Handled = true; //Doesn't work
    }

How can I handle all types of exceptions here so the code will attempt to continue? 我如何处理所有类型的异常,以便代码尝试继续?

Frankly, your entire design is a bad idea, in my opinion. 坦率地说,在我看来,你的整个设计都是个坏主意。 It appears to me that you want to be able to "handle" these exceptions by logging them and letting your program continue executing. 在我看来,您希望能够通过记录它们并让程序继续执行来“处理”这些异常。 But that's an incredibly dangerous approach and definitely not recommended. 但这是一种非常危险的方法,绝对不推荐。 You should only catch and handle exceptions for which you know in advance what the exception is and what the safe way to handle it is. 您应该只捕获并处理,而您知道异常提前的例外是什么和如何处理它是安全的方式。

To do otherwise is to risk leaving your program in an unknown state, leading to anything from (at best) buggy behavior to (at worst) permanently corrupting the state of user's important data. 否则就是冒着使程序处于未知状态的风险,从而导致从(最好)有缺陷的行为到(最坏的情况)永久性地破坏用户重要数据的状态。

See also Should you catch all exceptions? 另请参阅您是否应该捕获所有异常?

But, assuming you're going to do this anyway, you're not going to be able to use the LogUnhandledException() method to set the Handled property in the event args, because each of those events is different. 但是,假设您无论如何都要执行此操作,您将无法使用LogUnhandledException()方法在事件args中设置Handled属性,因为每个事件都是不同的。 Only the DispatcherUnhandledException event even has a Handled property to set. 只有DispatcherUnhandledException事件甚至具有要设置的Handled属性。 The UnobservedTaskException has an Observed property which you can set, and the AppDomain.UnhandledException event doesn't even have an analogous property. UnobservedTaskException具有可以设置的Observed属性, AppDomain.UnhandledException事件甚至没有类似的属性。

However, you can of course specialize each handler to do that. 但是,您当然可以专门处理每个处理程序。 For example: 例如:

protected override void OnStartup(StartupEventArgs e)
{
     AppDomain.CurrentDomain.UnhandledException += (s, ex) =>
     LogUnhandledException((Exception)ex.ExceptionObject, 
    "AppDomain.CurrentDomain.UnhandledException");

     DispatcherUnhandledException += (s, ex) =>
     {
         LogUnhandledException(ex.Exception, 
         "Application.Current.DispatcherUnhandledException");
         ex.Handled = true;
     };

     TaskScheduler.UnobservedTaskException += (s, ex) =>
     {
         LogUnhandledException(ex.Exception, 
         "TaskScheduler.UnobservedTaskException");
         ex.SetObserved();
     };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM