-1

Inherited WPF application sets up Dispatcher.UnhandledException in App.xaml.cs:

public App() : base()
{
    this.Dispatcher.UnhandledException += OnDispatcherUnhandledException;
}

Except am occasionally getting "program has stopped working" without OnDispatcherUnhandledException being called. As per much discussion in other threads, am adding the following:

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

but do not understand how that helps. Can anyone explain the difference between Dispatcher and CurrentDomain UnhandledException?

2 Answers2

1

It looks like you have exceptions occurring in another thread. You can debug CurrentDomain_UnhandledException to get an idea of where they're coming from. Dispatcher.UnhandledException handles exceptions on the specific UI thread associated with that Dispatcher. If you only have one UI thread (typically), it should handle those exceptions. If you have exceptions in other threads (most likely non-UI threads), those can be handled with AppDomain.CurrentDomain.UnhandledException.

http://blogs.microsoft.co.il/arik/2011/08/28/order-in-chaos-handling-unhandled-exceptions-in-a-wpf-application/

bcwhims
  • 2,655
  • 2
  • 15
  • 15
  • Since I don't yet have a stack trace, I don't know where the exception is originating. But I guess it's a reasonable inference that it's NOT in the UI thread? Hence implementing AppDomain.CurrentDomain.UnhandledException might catch it? Thanks. –  Nov 26 '18 at 19:45
0

You are likely getting any of the few exceptions that cannot be caught. E.g. StackOverflowException, ThreadAbortException and AccessViolationException (often from faulty native code).

Read this answer for more information.

l33t
  • 18,692
  • 16
  • 103
  • 180