在RaiseAndSetIfChanged中吞下异常

问题描述 投票:0回答:1

通常在反应式扩展中,任何未处理的异常都会冒泡并且通常会导致程序终止,并且除非订阅ThrownExceptions,否则ReactiveUI会通过重新抛出异常来执行此操作。因此,我惊讶地发现以下代码示例(在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没有订阅者而不是仅记录它,我本来希望重新抛出异常(参见第382行的https://github.com/reactiveui/ReactiveUI/blob/a4ee168dade8b5e3f34337fabd56eca10eca5200/src/ReactiveUI/ReactiveObject/IReactiveObjectExtensions.cs) 。

c# reactiveui
1个回答
0
投票

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

我建议你提交一个bug,因为异常应该传播

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

© www.soinside.com 2019 - 2024. All rights reserved.