Malcolm
Malcolm

Reputation: 12874

WPF - DispatcherUnhandledException does not seem to work

I started a new WPF project in VS2008 and then added some code to trap DispatcherUnhandledException. Then I added a throw exception to Window1 but the error is not trapped by the handler. Why?

   public App()
    {
        this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);

    }

    void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        System.Windows.MessageBox.Show(string.Format("An error occured: {0}", e.Exception.Message), "Error");
        e.Handled = true;
    }

 void Window1_MouseDown(object sender, MouseButtonEventArgs e)
    {
        throw new NotImplementedException();
    }

Upvotes: 12

Views: 19342

Answers (5)

irfn
irfn

Reputation: 560

Look at following Microsoft Learn link https://learn.microsoft.com/en-us/dotnet/api/system.windows.application.dispatcherunhandledexception?view=windowsdesktop-8.0 Following is Relevant here

If an exception is not handled on either a background user interface (UI) thread (a thread with its own Dispatcher) or a background worker thread (a thread without a Dispatcher), the exception is not forwarded to the main UI thread. Consequently, DispatcherUnhandledException is not raised. In these circumstances, you will need to write code to do the following:

  1. Handle exceptions on the background thread.
  2. Dispatch those exceptions to the main UI thread.
  3. Rethrow them on the main UI thread without handling them to allow DispatcherUnhandledException to be raised.

Upvotes: 3

Matt R
Matt R

Reputation:

At first, even outside the debugging environment, my handler does not seem to be triggering.....then I realized I forget to set e.Handled = true.

In truth it was working but because e.Handled was still false the standard exception handler still kicked in and did its thing.

Once I set e.Handled = true, then everything was hunky dory. So if its not working for you, make sure you've done that step.

Upvotes: 2

Joel
Joel

Reputation: 41

This is how I handle it. This isn't pretty but keep in mind that this type of error should never make it past debugging as a dev. Those errors should be long resolved before you go to production (so its okay that this isn't pretty). In the Startup project, in the App.xaml (App.xaml.cs) code behind, I put the following code.

  • OnStartup, create a DispatcherUnhandledException event handler
  • In the handler, use a MessageBox to display the message. Note that its likely the startup window has not yet been created so don't try to put it in a window.
  • e.Handle the error
  • I like to see when there are additional internal errors so I continue to call the display window until the internal error is null.

I'm not sure why the code block special characters aren't formatting this correctly. Sorry about that.

protected override void OnStartup(StartupEventArgs e)
    {
        // define application exception handler
        Application.Current.DispatcherUnhandledException += 
            AppDispatcherUnhandledException;

        // defer other startup processing to base class
        base.OnStartup(e);
    }

    private void AppDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        runException(e.Exception);

        e.Handled = true;   
    }

    void runException(Exception ex)
    {
        MessageBox.Show(
            String.Format(
                "{0} Error:  {1}\r\n\r\n{2}",
                ex.Source, ex.Message, ex.StackTrace,
                "Initialize Error",
                MessageBoxButton.OK, 
                MessageBoxImage.Error));

        if (ex.InnerException != null)
        {
            runException(ex.InnerException);
        }
    }

Upvotes: 4

Dominic Hopton
Dominic Hopton

Reputation: 7292

This can happen because of the way you have the debugger handling exceptions -- Debug/Exceptions... should allow you to configure exactly how you want it handled.

Upvotes: 6

Malcolm
Malcolm

Reputation: 12874

For those interested

It seems that the IDE is still breaking on exceptions and that if you click continue in the IDE it call the error handler.

Upvotes: 1

Related Questions