DispatcherUnhandledException 和 AppDomain.UnhandledException 何时不足(WPF 应用程序)?
我已经连接了这两个,但有时我的应用程序仍然崩溃,只是弹出 Windows“发生异常”对话框。当我单击“确定”时,应用程序就会退出。在什么情况下这两个处理程序(在 WPF 应用程序中)不会捕获异常?
我应该提到我正在做大量 COM 互操作,所以这总是值得怀疑的:-)
I've hooked both of these, but occasionally my app still crashes and just throws up the Windows "an exception has occurred" dialog. When I click OK, the app just exits. Under what circumstances won't exceptions get caught with these two handlers (in a WPF app)?
I should mention that I'm doing a bunch of COM interop, so that's always suspect :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其中任何一个都不会引发未处理的异常有几个原因。
Dispatcher.UnhandledException
仅针对调度程序外观中发生的异常(其中方法是通过 Invoke 或 BeginInvoke 调用输入的)引发(文档)。因此,未以这种方式调用的其他线程或方法上的异常将错过此事件处理程序。.Net 还在 4.0 中对进入 AppDomain.UnhandledException 事件的异常进行了更改。访问冲突和堆栈溢出等损坏的状态异常不再通过此事件。
我不确定其中任何一个是否与您相关。最简单的检查方法是附加一个调试器并查看它产生什么输出。
There are a couple of reasons why an unhandled exception would not be raised by either of these.
The
Dispatcher.UnhandledException
is only raised for exceptions that occur in the look of the dispatcher where the method was entered via an Invoke or BeginInvoke call (documentation). So exceptions on other threads or methods that were not invoked in this way will miss this event handler..Net also made a change in 4.0 to what exceptions will go into the
AppDomain.UnhandledException
event. Corrupted state exceptions such as access violations and stack overflows no longer go through this event.I'm not sure if either of these will be relevant to you. The simplest way to check is to just attach a debugger and see what output it produces.
.NET 中存在无法捕获的异常。这些是 CLR 在进入它认为完全不可恢复的状态时直接引发的异常。 StackOverflowException 就是一个例子。 Microsoft 的理念是,这种状态不是可能的异常情况,它一定是代码中的错误,因此不应处理。在这种情况下,异常将立即传播到应用程序关闭错误消息。
There are uncatchable exceptions in .NET. These are exceptions thrown directly by the CLR when it gets into a state that it considers to be completely unrecoverable. One example is the StackOverflowException. Microsoft's philosophy is that this state isn't a possible exceptional circumstance, it must be a bug in your code and therefore shouldn't be handled. In this case the exception will immediately propagate right the way up to a application closing error message.