c# - 如何捕获 ReactiveCommand 异常?

标签 c# exception reactiveui

我有一个基本的 ReactiveCommand。没有异步魔法,只有普通的 ReactiveCommand.Create()。我 Subscribe() 的重载采用异常处理程序,但从未在所述异常处理程序中遇到断点(我没想到会这样)。我订阅了 ThrownErrors,也从未在该异常处理程序中遇到断点(我有点预料到这一点)。

示例代码如下:

var myCommand = ReactiveCommand.Create();

// this does not seem to work
myCommand.Subscribe(
    _ => { throw new Exception("oops"); },
    ex => { 
            Console.WriteLine(ex.Mesage);
            Debugger.Break(); 
          });

//this does not seem to work either
myCommand.ThrownExceptions.Subscribe(
    ex => { 
            Console.WriteLine(ex.Mesage);
            Debugger.Break();
          });

我做了功课,检查了题目中的问题和答案。

ReactiveUI exception handling

How to catch exception from ReactiveCommand?

我也检查了邮件列表,发现了这个: https://groups.google.com/forum/#!topic/reactivexaml/Dkc-cSesKPY

所以我决定将其更改为某种异步解决方案:

var myCommand = ReactiveCommand.CreateAsyncObservable(_ => this.Throw());
myCommand.Subscribe(
    _ => { Console.WriteLine("How did we get here?"); },
    // this is not expected to work
    ex => { 
            Console.WriteLine(ex.Message);
            Debugger.Break();
          });

// however, I sort of expect this to work
myCommand.ThrownExceptions.Subscribe(
    ex => {
            Console.WriteLine(ex.Message);
            Debugger.Break();
          });

[...]

private IObservable<object> Throw()
{
    Debugger.Break();
    throw new Exception("oops");
}

然而,除了 Throw() 方法中的断点外,我从未遇到过任何断点。 :(

我做错了什么?我应该如何捕获此处的异常?

编辑:

但是,当我从可观察对象中抛出异常时,我确实遇到了异常处理程序断点,就像这样

private IObservable<object> Throw()
{
    Debugger.Break();
    return Task.Factory.StartNew(() =>
            {
                throw new Exception("oops");
                return new object();
            }).ToObservable();
}

问题修改为:“我是否能够处理方法内部的异常而不是可观察的异常?”

最佳答案

这是 ReactiveUI 当前版本中的一个错误 - 创建“执行”可观察对象时抛出的异常被吞噬,ReactiveCommand 的内部状态处于损坏状态。这已在下一版本中修复。

参见 this github issue了解详情。

关于c# - 如何捕获 ReactiveCommand 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36182904/

相关文章:

C# - 从 List3 中删除 List1 和 List2 中的项目时出现问题

c# - Web.Optimizations - 有什么方法可以从 Style/Script Bundle 中获取所有内容?

java - 无意义的 Java 异常?

java - TypeNotPresentExceptionProxy

swift - 致命异常: NSInternalInconsistencyException while using formatter to convert string to date

c# - ReactiveUI ObservableAsPropertyHelper 与普通支持变量

c# - ReactiveUI ObservableForProperty 生命周期

c# - 想知道如何完成一个函数,让控制台从包含 20 个答案的数组中写出一个随机字符串?

C# 解析文本 block

.net - ReactiveUI ObservableAsPropertyHelper/Reactive Extensions 内存泄漏?