It seems an unhandled exception will crash the program only if it's in the UI thread. Exceptions in other threads fail silently. I want to be able to either exit the application or display info about the exception when exceptions are thrown in these threads. Unfortunately the UnhandledException
event only seems to work for one thread.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Task.Run(() => throw new Exception("This is an exception."));
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show(e.ExceptionObject.ToString());
}
This neither crashes the application nor notifies me of the exception. Is there a way to make sure exceptions across all threads never cause silent failures, either using code or with some kind of application setting?
What is the best way to catch exception in Task? does not address using global exception handling to prevent silent failures, and so it is very different from my question.