通常在反应式扩展中,任何未经处理的异常都会冒泡并通常导致程序终止,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没有订阅者而不仅仅是记录它,我本来希望重新抛出异常(请参阅第382行的 https://github.com/reactiveui/ReactiveUI/blob/a4ee168dade8b5e3f34337fabd56eca10eca5200/src/ReactiveUI/ReactiveObject/IReactiveObjectExtensions.cs(。
https://reactiveui.net/docs/handbook/default-exception-handler/
我建议您提交一个错误,因为异常应该传播
值得注意的是,ObservableForProperty主要用于内部使用。