本文翻译自:WPF global exception handler [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
- Globally catch exceptions in a WPF application? 在WPF应用程序中全局捕获异常? 6 answers 6个答案
Sometimes, under not reproducible circumstances, my WPF application crashes without any message. 有时,在不可复制的情况下,我的WPF应用程序崩溃而没有任何消息。 The application simply close instantly. 该应用程序只是立即关闭。
Where is the best place to implement the global Try/Catch block. 哪里是实现全局Try / Catch块的最佳位置。 At least I have to implement a messagebox with: "Sorry for the inconvenience ..." 至少我必须用以下消息实现消息框:“抱歉给您带来的不便...”
#1楼
参考:https://stackoom.com/question/6B3y/WPF全局异常处理程序
#2楼
You can handle the AppDomain.UnhandledException
event 您可以处理AppDomain.UnhandledException
事件
EDIT: actually, this event is probably more adequate: Application.DispatcherUnhandledException
编辑:实际上,此事件可能更合适: Application.DispatcherUnhandledException
#3楼
You can trap unhandled exceptions at different levels: 您可以在不同级别捕获未处理的异常:
-
AppDomain.CurrentDomain.UnhandledException
From all threads in the AppDomain.AppDomain.CurrentDomain.UnhandledException
来自AppDomain.CurrentDomain.UnhandledException
所有线程。 -
Dispatcher.UnhandledException
From a single specific UI dispatcher thread.Dispatcher.UnhandledException
来自单个特定的UI调度程序线程。 -
Application.Current.DispatcherUnhandledException
From the main UI dispatcher thread in your WPF application.Application.Current.DispatcherUnhandledException
来自WPF应用程序中的主 UI调度程序线程。 -
TaskScheduler.UnobservedTaskException
from within each AppDomain that uses a task scheduler for asynchronous operations. 每个使用任务计划程序进行异步操作的AppDomain中的TaskScheduler.UnobservedTaskException
。
You should consider what level you need to trap unhandled exceptions at. 您应该考虑需要在什么级别捕获未处理的异常。
Deciding between #2 and #3 depends upon whether you're using more than one WPF thread. 在#2和#3之间进行选择取决于您是否使用多个WPF线程。 This is quite an exotic situation and if you're unsure whether you are or not, then it's most likely that you're not. 这是一个非常特殊的情况,如果您不确定自己是否在,那么很可能你不是。
#4楼
为了补充托马斯的答案, Application
类还具有您可以处理的DispatcherUnhandledException
事件。
#5楼
In addition to the posts above: 除了以上职位:
Application.Current.DispatcherUnhandledException
will not catch exceptions that are thrown from another thread then the main thread. 不会捕获从其他线程(然后是主线程)引发的异常。 You have to handle those exceptions on its actual Thread. 您必须在其实际线程上处理这些异常。 But if you want to Handle them on your global exception handler you can pass it to the main thread: 但是,如果要在全局异常处理程序上处理它们,则可以将其传递给主线程:
System.Threading.Thread t = new System.Threading.Thread(() =>
{
try
{
...
//this exception will not be catched by
//Application.DispatcherUnhandledException
throw new Exception("huh..");
...
}
catch (Exception ex)
{
//But we can handle it in the throwing thread
//and pass it to the main thread wehre Application.
//DispatcherUnhandledException can handle it
System.Windows.Application.Current.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action<Exception>((exc) =>
{
throw new Exception("Exception from another Thread", exc);
}), ex);
}
});
#6楼
As mentioned above 正如刚才提到的
Application.Current.DispatcherUnhandledException will not catch exceptions that are thrown from another thread then the main thread. Application.Current.DispatcherUnhandledException将不会捕获从另一个线程(然后是主线程)引发的异常。
That actual depend on how the thread was created 实际情况取决于线程的创建方式
One case that is not handled by Application.Current.DispatcherUnhandledException is System.Windows.Forms.Timer for which Application.ThreadException can be used to handle these if you run Forms on other threads than the main thread you will need to set Application.ThreadException from each such thread Application.Current.DispatcherUnhandledException未处理的一种情况是System.Windows.Forms.Timer,如果您在除主线程之外的其他线程上运行Forms,则可以使用Application.ThreadException来处理这些情况,您需要设置Application.ThreadException从每个这样的线程