在 RaiseAndSetIfChanged 中吞下异常

exception swallowed in RaiseAndSetIfChanged

通常在响应式扩展中,任何未处理的异常都会冒泡并通常导致程序终止,并且 ReactiveUI 通过重新抛出异常来遵循这一点,除非订阅了 ThrownExceptions。因此,我很惊讶地看到以下代码示例(适用于 RoslynPad)实际上并未终止:

#r "nuget:ReactiveUI/9.13.1"

using System.Reactive.Linq;
using ReactiveUI;

class ReactiveExample : ReactiveObject
{
    public ReactiveExample()
    {
        var o = this.ObservableForProperty(x => x.S, skipInitial: true).Select(x => x.Value);
        o.Subscribe(s => 
        {
            Console.WriteLine("received value " + s);
            throw new Exception("throw on value " + s);
        });
        //this.ThrownExceptions.Subscribe(e => throw new Exception("uncaught", e));
    }

    public string S
    {
        get => _s; 
        set => this.RaiseAndSetIfChanged(ref _s, value);
    }

    private string _s = "";
}

var r = new ReactiveExample();
r.S = "bar";

如果你在上面对应行注释订阅了ThrownExceptions,很明显确实抛出了异常。

这是错误还是功能?我相信这是由 ReactiveUI.IReactiveObjectExtensions.NotifyObservable 中的 try/catch 引起的,如果 ThrownExceptions observable 没有订阅者而不是仅仅记录它,我本来希望重新抛出异常(参见 https://github.com/reactiveui/ReactiveUI/blob/a4ee168dade8b5e3f34337fabd56eca10eca5200/src/ReactiveUI/ReactiveObject/IReactiveObjectExtensions.cs 来自第 382 行)。

https://reactiveui.net/docs/handbook/default-exception-handler/

我建议您提交错误,因为异常应该传播

值得注意的是 ObservableForProperty 主要供内部使用。