在阅读了一些关于AppDomain.UnhandledException和Application.DispatcherUnhandledException之间差异的优秀帖子之后,看来我应该同时处理它们。这是因为用户可以从主UI线程抛出的异常(即Application.DispatcherUnhandledException)中恢复的可能性更大。正确的吗?
另外,我是否应该让用户有机会继续这两个程序,或者仅仅是Application.DispatcherUnhandledException?
下面的示例代码处理AppDomain.UnhandledException和Application.DispatcherUnhandledException,并且两者都为用户提供了尝试继续的选项,尽管有异常。
[谢谢,下面的一些代码从其他答案中解脱出来]
的App.xaml
<Application x:Class="MyProgram.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="App_StartupUriEventHandler"
Exit="App_ExitEventHandler"
DispatcherUnhandledException="AppUI_DispatcherUnhandledException">
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.cs [编辑]
/// <summary>
/// Add dispatcher for Appdomain.UnhandledException
/// </summary>
public App()
: base()
{
this.Dispatcher.UnhandledException += OnDispatcherUnhandledException;
}
/// <summary>
/// Catch unhandled exceptions thrown on the main UI thread and allow
/// option for user to continue program.
/// The OnDispatcherUnhandledException method below for AppDomain.UnhandledException will handle all other exceptions thrown by any thread.
/// </summary>
void AppUI_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
if (e.Exception == null)
{
Application.Current.Shutdown();
return;
}
string errorMessage = string.Format("An application error occurred. If this error occurs again there seems to be a serious bug in the application, and you better close it.\n\nError:{0}\n\nDo you want to continue?\n(if you click Yes you will continue with your work, if you click No the application will close)", e.Exception.Message);
//insert code to log exception here
if (MessageBox.Show(errorMessage, "Application User Interface Error", MessageBoxButton.YesNoCancel, MessageBoxImage.Error) == MessageBoxResult.No)
{
if (MessageBox.Show("WARNING: The application will close. Any changes will not be saved!\nDo you really want to close it?", "Close the application!", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) == MessageBoxResult.Yes)
{
Application.Current.Shutdown();
}
}
e.Handled = true;
}
/// <summary>
/// Catch unhandled exceptions not thrown by the main UI thread.
/// The above AppUI_DispatcherUnhandledException method for DispatcherUnhandledException will only handle exceptions thrown by the main UI thread.
/// Unhandled exceptions caught by this method typically terminate the runtime.
/// </summary>
void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
string errorMessage = string.Format("An application error occurred. If this error occurs again there seems to be a serious bug in the application, and you better close it.\n\nError:{0}\n\nDo you want to continue?\n(if you click Yes you will continue with your work, if you click No the application will close)", e.Exception.Message);
//insert code to log exception here
if (MessageBox.Show(errorMessage, "Application UnhandledException Error", MessageBoxButton.YesNoCancel, MessageBoxImage.Error) == MessageBoxResult.No)
{
if (MessageBox.Show("WARNING: The application will close. Any changes will not be saved!\nDo you really want to close it?", "Close the application!", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) == MessageBoxResult.Yes)
{
Application.Current.Shutdown();
}
}
e.Handled = true;
}
答案 0 :(得分:38)
AppDomain.CurrentDomain.UnhandledException
捕获appdomain所有线程上的所有异常。但我发现这非常不可靠。 Application.Current.DispatcherUnhandledException
捕获UI线程上的所有异常。这似乎可靠地工作,并将替换UI线程上的AppDomain.CurrentDomain.UnhandledException
处理程序(优先级)。使用e.Handled = true
保持应用程序正常运行。
为了捕获其他线程上的异常(在最好的情况下,它们是在自己的线程上处理的),我发现System.Threading.Tasks.Task(仅限.NET 4.0及更高版本)是低维护的。使用方法.ContinueWith(...,TaskContinuationOptions.OnlyOnFaulted)
处理任务中的异常。有关详细信息,请参阅我的回答here。
答案 1 :(得分:5)
AppDomain.UnhandledException
处理程序连接为:
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
但是我找不到一种方法来标记处理程序中的处理 - 所以这似乎总会导致应用程序关闭,无论你做什么。所以我认为这不是很多用途。
最好处理Application.Current.DispatcherUnhandledException
并测试CommunicationObjectFaultedException
- 因为只需重新初始化代理就可以从中恢复 - 就像在初始连接时一样。例如:
void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {
if (e.Exception is CommunicationObjectFaultedException) { //|| e.Exception is InvalidOperationException) {
Reconnect();
e.Handled = true;
}
else {
MessageBox.Show(string.Format("An unexpected error has occured:\n{0}.\nThe application will close.", e.Exception));
Application.Current.Shutdown();
}
}
public bool Reconnect() {
bool ok = false;
MessageBoxResult result = MessageBox.Show("The connection to the server has been lost. Try to reconnect?", "Connection lost", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
ok = Initialize();
if (!ok)
Application.Current.Shutdown();
}
其中Initialize具有您的初始代理实例化/连接代码。
在您上面发布的代码中,我怀疑您正在处理DispatcherUnhandledException
两次 - 通过在xaml和代码中连接处理程序。