2

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.

Kyle Delaney
  • 11,616
  • 6
  • 39
  • 66
  • You might try [searching for an answer](https://www.bing.com/search?q=how+to+catch+exceptions+from+tasks) first...https://msdn.microsoft.com/en-us/library/dd537614(v=vs.110).aspx – Rufus L Sep 01 '17 at 20:20
  • 2
    @Rufus I did. You might notice those results are worded rather differently from my question, so they didn't turn up in my searches. You might also notice that they neither address nor answer my question. I'm not interested in ordinary exception handling for threads. I'm trying to implement some kind of global exception handling to avoid silent failures. Something akin to `AppDomain.CurrentDomain.UnhandledException` as my question illustrates. – Kyle Delaney Sep 01 '17 at 21:12

0 Answers0