首页 > 解决方案 > 在 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 行开始)。

标签: c#reactiveui

解决方案


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

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

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


推荐阅读