what is the best way to catch wpf application errors. I have already added the following to my app.xaml.cs:
void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendFormat("{0}\n", e.Exception.Message);
stringBuilder.AppendFormat(
"Exception handled on main UI thread {0}.", e.Dispatcher.Thread.ManagedThreadId);
var result = MessageBox.Show(
"Application must exit:\n\n" + stringBuilder.ToString() + "\n\nSave before exit?",
"app",
MessageBoxButton.YesNo,
MessageBoxImage.Error);
if (result == MessageBoxResult.Yes)
{
MessageBox.Show(e.Exception.InnerException.ToString());
MessageBox.Show(e.Exception.InnerException.Message.ToString());
}
this.Shutdown(-1);
e.Handled = true;
}
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = e.ExceptionObject as Exception;
MessageBox.Show(ex.Message, "Uncaught Thread Exception", MessageBoxButton.OK, MessageBoxImage.Error);
}
But I still get cases where the application crashes and the error does not appear in the message box. How do I debug such errors? The windows event log does not show me any useful information. And of course, the application works fine in the IDE, so that doesn't help either.
source
share