App()
{
this.Startup += App_Startup;
}
private void App_Startup(object sender, StartupEventArgs e)
{
this.DispatcherUnhandledException += App_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
}
//主线程未处理异常
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
//HandyControl.Controls.Growl.Error($"应用程序遇到未处理的异常:{e.Exception.Message}");
e.Handled = true;
}
//未处理线程异常(如果主线程未处理异常已经处理,该异常不会触发)
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception ex)
{
//HandyControl.Controls.Growl.Error($"应用程序遇到未处理的异常:{ex.Message}");
}
}
//未处理的Task内异常
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
//HandyControl.Controls.Growl.Error($"应用程序遇到未处理的异常:{e.Exception.Message}");
}
标签:全局,App,object,private,void,wpf,异常,捕获,sender
From: https://www.cnblogs.com/ives/p/18219844