tags:

views:

919

answers:

5

Hi

I started a new WPF project in VS2008 and then add code to trap DispatcherUnhandledException.

Then I add throw an 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();
    }

Malcolm

A: 

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.

Malcolm
+1  A: 

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.

dhopton
A: 

Were you every able to resolve this? I've found the same issue.

A: 

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.

+1  A: 

Look at following msdn link http://msdn.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx 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.
irfn