.NET 中 UnhandledException 和 DispatcherUnhandledException 的区别
.NET 中的 AppDomain.UnhandledException 和 Application.DispatcherUnhandledException 之间有什么区别?
我需要一个在发生任何未处理的异常时触发的事件。我遇到过这两个,但我不知道他们有什么不同。另外,有没有被解雇的情况?
What is the difference between AppDomain.UnhandledException and Application.DispatcherUnhandledException in .NET?
I need an event that is fired when any unhandled exception occurs. I have come across these two, but I dont know in what ways they differ from each other. Also, are there cases when they are not fired?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Application.DispatcherUnhandledException
将处理 WPF 应用程序中主 UI 线程上引发的异常。AppDomain.UnhandledException
将处理在任何线程上抛出且从未捕获的异常。这包括您手动创建的线程或控制台应用程序中的主线程。 WPF 正在捕获 UI 线程上的异常,因此您不会在 AppDomain.UnhandledException 中看到这些异常。另请注意,未处理的异常通常会终止运行时,因此在引发 AppDomain.UnhandledException 后,您的程序将立即退出。相反,
Application.DispatcherUnhandledException
正在捕获异常并让您的程序继续运行。Application.DispatcherUnhandledException
will handle exceptions thrown on the main UI thread in a WPF application.AppDomain.UnhandledException
will handle exceptions thrown on any thread and never caught. This includes threads you create manually or the main thread in a Console application. WPF is catching the exceptions on the UI thread, so you will not see those inAppDomain.UnhandledException
.Also note that unhandled exceptions typically terminate the runtime, so after
AppDomain.UnhandledException
is raised your program will immediately exit. In contrast,Application.DispatcherUnhandledException
is catching exceptions and will let your program continue.DispatcherUnhandledException 仅由 UI 线程引发,并且仅当运行事件时引发异常时引发。有一些专门处理此类异常的传统,Windows 窗体也有 Application.ThreadException(命名不好,与线程无关)。
原因是,处理异常并使程序保持活动状态的机会很小,因为 UI 事件处理程序并不总是会大幅改变程序状态。这需要大量的一厢情愿的想法。 Windows 窗体将这一点发挥到了极致,它显示一个带有“继续”按钮的 ThreadExceptionDialog,允许用户忽略异常。 WPF 不这样做,您必须自己编写一个这样的对话框。这就是该事件发生的原因。
DispatcherUnhandledException 的默认操作是不捕获异常。所以你可以忽略它,AppDomain.UnhandledException 接下来会触发。
DispatcherUnhandledException is raised only by the UI thread and only if an exception was raised while running an event. There's a bit of a tradition to handle these kind of exceptions specially, Windows Forms has it too with Application.ThreadException (poorly named, nothing to do with threads).
The reason is that there is a minor chance to handle the exception and keep the program alive since UI event handlers don't always mutate the state of program too dramatically. This takes large helpings of wishful thinking. Windows Forms takes this to an extreme, it displays a ThreadExceptionDialog that has a Continue button, allowing the user to ignore the exception. WPF does not do that, you'd have to write a dialog like that yourself. Which is why the event is there.
The default action of DispatcherUnhandledException is to not catch the exception. So you're okay to ignore it, AppDomain.UnhandledException will fire next.
说:
“对于主 UI 线程上运行的代码未处理的每个异常,应用程序都会引发 DispatcherUnhandledException。”
表示:
“此事件可以在任何应用程序域中处理。但是,该事件不一定在发生异常的应用程序域中引发。”
因此 DispatcherUnhandledException 适用于 UI 线程异常,而 AppDomain.UnhandledException 适用于其他所有异常。
希望有帮助!
says:
"DispatcherUnhandledException is raised by an Application for each exception that is unhandled by code running on the main UI thread."
says:
"This event can be handled in any application domain. However, the event is not necessarily raised in the application domain where the exception occurred."
So DispatcherUnhandledException is for UI thread exceptions, and AppDomain.UnhandledException is for everything else.
Hope that helps!