如何捕获 ReactiveCommand 异常?

How to catch ReactiveCommand Exceptions?

我有基本的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

我也查看了邮件列表,发现了这个: 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();
}

问题修改为:"am I capable of handling an exception from within the method and not the observable?"

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

详情见this github issue