1

Given a WPF Core/Framework application. Unhandled Exceptions should be caught by some UnhandledException event.

TaskScheduler.UnobservedTaskException += (s, e) => Debugger.Break();
AppDomain.CurrentDomain.UnhandledException += (sender, eventArgs) => Debugger.Break();
Dispatcher.UnhandledException += (sender, args) => Debugger.Break();
DispatcherUnhandledException += (sender, eventArgs) => Debugger.Break();

Given is an ObservableCollection<TItem> collection presented by a combobox. The collection also fires ItemPropertyChanged events.

Collection.CollectionChanged += ItemsCollectionChanged;

public event PropertyChangedEventHandler PropertyItemChanged = delegate { };     

private void ItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.OldItems != null)
    {
        foreach (INotifyPropertyChanged item in e.OldItems)
            item.PropertyChanged -= ItemPropertyChanged;
    }

    if (e.NewItems != null)
    {
        foreach (INotifyPropertyChanged item in e.NewItems)
            item.PropertyChanged += ItemPropertyChanged;
    }
}

The issue was I didn't implement

private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    throw new NotImplementedException("ToDo");
    // done later PropertyItemChanged(sender, e);
}

When I change the collection item property from code behind/view model like this

ComboBoxCatalog.Collection.First().IsChecked = true;

everything is fine. The debugger stops on some UnhandledException.

Now when I change an property by combobox:

<ComboBox ItemsSource="{Binding ComboBoxCatalog.Collection, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
    <ComboBox.ItemTemplate>
        <DataTemplate DataType="{x:Type local:CheckboxItem}">
            <CheckBox Content="{Binding IsChecked}" IsChecked="{Binding IsChecked}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

None of these UnhandledException are fired. Only FirstChanceException are fired.

I have implemented a full working demo on Github: https://github.com/LwServices/WpfAppExceptionDemo

What need to be done that the exception will fire UnhandledException ?

Update: I get the same behavior on .NET Core 3.1 and .NET Framework 4.7.2

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
LWS
  • 329
  • 3
  • 15
  • 2
    The binding will catch the exception and turn it into a validation error (which you can inspect through e.g. `Validation.HasError`). It isn't bubbled up to the dispatcher/AppDomain, and it doesn't crash your app. See `Binding.ValidatesOnExceptions` – canton7 Jun 01 '20 at 08:34

1 Answers1

1

The exception is catched and handled for you by the framework.

If you look in the output window while you are debugging in Visual Studio, you'll see that the debugger outputs a System.Windows.Data Error message:

System.Windows.Data Error: 8 : Cannot save value from target back to source. ...

If you want to turn this kind of errors into runtime exceptions that you can catch yourself in your global error handler, you could implement your own TraceListener as suggested here.

mm8
  • 163,881
  • 10
  • 57
  • 88