在框架级别中捕获异常WPF [英] Catching Exceptions in WPF at the FrameWork Level

查看:180
本文介绍了在框架级别中捕获异常WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个轻量级的WPF MVVM框架,并希望能够赶上未处理的异常,并从他们理想中恢复过来。

I'm developing a light-weight WPF MVVM framework, and would like to be able to catch unhandled exceptions, and ideally recover from them.

忽略的那一刻所有的好论据不这样做,我会遇到以下情况:

Ignoring for the moment all the good arguments for not doing this, I encounter the following situation:

如果我的应用程序的方法OnStartup内注册AppDomain.CurrentDomain.UnhandledException处理程序。 xaml.cs,如下...

If I register a handler for AppDomain.CurrentDomain.UnhandledException within the OnStartup method of the App.xaml.cs, as follows...

App.xaml.cs:

App.xaml.cs:

protected override void OnStartup(StartupEventArgs e)
{
  AppDomain.CurrentDomain.UnhandledException += new
     UnhandledExceptionEventHandler(this.AppDomainUnhandledExceptionHandler); 

  base.OnStartup(e);
}


 void AppDomainUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs ea)
{
  Exception e = (Exception)ea.ExceptionObject;
  // log exception
}



然后之一中引发异常我的虚拟机,如预期的那样处理程序被调用。

and then raise an exception within one of my VM's, the handler is called as expected.

到目前为止好,除了事实,有没有办法,我可以恢复使用这种方法,所有我能做的就是记录异常,然后让CLR终止应用程序。

So far so good, except for the fact that there is no way that I can recover using this approach, all that I can do is log the exception and then let the CLR terminate the app.

我其实是想要做的就是恢复,并将控制权返回到主framwork VM。 (再次搁置反对这样做的动机)。

What I actually wanted to do is to recover, and return control to the main framwork VM. (Again putting aside the motivations against doing this).

所以,在做一些阅读,我决定在同一个地方注册AppDomain.CurrentDomain.UnhandledException事件处理程序,这样的代码现在看起来是这样的...

So, doing some reading, I decide to register an event handler for AppDomain.CurrentDomain.UnhandledException in the same place, so that the code now looks something like this...

protected override void OnStartup(StartupEventArgs e)
{
  AppDomain.CurrentDomain.UnhandledException += 
    new UnhandledExceptionEventHandler(this.AppDomainUnhandledExceptionHandler); 

  this.DispatcherUnhandledException += 
    new DispatcherUnhandledExceptionEventHandler(DispatcherUnhandledExceptionHandler);

  base.OnStartup(e);
}

void AppDomainUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs ea)
{
  Exception e = (Exception)ea.ExceptionObject;
  // log exception
}

void DispatcherUnhandledExceptionHandler(object sender, DispatcherUnhandledExceptionEventArgs args)
{
  args.Handled = true;
  // implement recovery
}



问题是,一旦我注册对于this.DispatcherUnhandledException处理,既不事件处理函数。因此,注册DispatcherUnhandledExceptionHandler不知何故取消对AppDomain.CurrentDomain.UnhandledException处理程序。

The issue is that once I register the handler for this.DispatcherUnhandledException, NEITHER EVENT HANDLER IS CALLED. So registering the DispatcherUnhandledExceptionHandler somehow deactivates the handler for AppDomain.CurrentDomain.UnhandledException.

有没有人有醒目和未处理的异常VM恢复的方法?

Does anyone have an approach for catching and recovering from unhandled VM exceptions ?

一提的是,没有明确使用了框架线程的它可能是很重要的。

It might be important to mention that there is no explicit use of threading in the framework.

推荐答案

究其原因VS显示了异常是因为你设置它像(无论你这样做明确或 - 更容易 - 在VS的默认配置是这样的)。您可以控制在遇到通过的调试 - >的调试代码的异常的Visual Studio做什么;例外菜单。

The reason VS shows you the exception is because you have set it up like that (either you did this explicitly or - more likely - the defaults in VS configured it like this). You can control what Visual Studio does when it encounters an exception in the debugged code through the Debug->Exceptions menu.

您甚至可以让它打破,即使你有这方面的渔获物在某些情况下,会非常方便。

You can even make it break even though you have a catch for it which is quite handy in some cases.

如果你不使用多线程,那么你应该罚款与DispatcherUnhandledException事件,因为它会赶上一切,得到主UI线程未捕获的。

If you're not using multi threading then you should be fine with the DispatcherUnhandledException event since it will catch everything that gets uncaught on the main UI thread.

这篇关于在框架级别中捕获异常WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆