c# - 在 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 主要用于内部使用。
推荐阅读
- python - 如何将初始数据放在 CreateView 中的表单上?
- python-3.x - Tensorflow 对象检测 API - 推理后如何读取 TFRecord?
- python - 输入到 CNN 的超像素
- c - 输入字符串由空终止字符组成
- slack - Slack conversations.list 方法没有列出所有频道
- firebase - 使用数据库时看不到 Firebase 的网络调用?
- java - 在 Java 中引用对象
- android - Kotlin CountDownTimer lateinit 属性尚未初始化且未找到 CoreComponentFactory
- elasticsearch - Elasticsearch,映射类型字符串不能包含启用的false 为什么?
- swift - UITABLE view sections with different prototype cells in each section