问题描述
好吧,这很简单:
Application.ThreadException
和AppDomain.CurrentDomain.UnhandledException
有什么区别??
What's the difference between
Application.ThreadException
andAppDomain.CurrentDomain.UnhandledException
?
我需要同时处理这两种情况吗?
Do I need to handle both?
谢谢!
推荐答案
Application.ThreadException 特定于 Windows 窗体.Winforms 运行事件处理程序以响应 Windows 发送给它的消息.以 Click 事件为例,我相信您知道它们.如果这样的事件处理程序抛出异常,则 Winforms 消息循环内有一个捕获该异常的后端.
Application.ThreadException is specific to Windows Forms. Winforms runs event handlers in response to messages sent to it by Windows. The Click event for example, I'm sure you know them. If such an event handler throws an exception then there's a back-stop inside the Winforms message loop that catches that exception.
该支持会触发 Application.ThreadException 事件.如果您不覆盖它,用户将获得 ThreadExceptionDialog.这允许他忽略异常并继续运行您的程序.顺便说一句,这不是个好主意.
That backstop fires the Application.ThreadException event. If you don't override it, the user will get a ThreadExceptionDialog. Which allows him to ignore the exception and keep running your program. Not a great idea btw.
您可以通过调用 Application.SetUnhandledExceptionMode() 在 Program.cs 的 Main() 方法中.如果没有适当的支持,通常会在线程因未处理的异常而终止时发生:AppDomain.UnhandledException 触发,程序终止.
You can disable this behavior by calling Application.SetUnhandledExceptionMode() in the Main() method in Program.cs. Without that backstop in place, the usual thing happens when a thread dies from an unhandled exception: AppDomain.UnhandledException fires and the program terminates.
Fwiw:ThreadException"是一个非常糟糕的名称选择.它与线程无关.
Fwiw: "ThreadException" was a very poor name choice. It has nothing to do with threads.
这篇关于Application.ThreadException 和 AppDomain.CurrentDomain.UnhandledException 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!