2016-03-23 135 views
0

我有一个基本的ReactiveCommand。没有异步巫术,只是普通的旧版本ReactiveCommand.Create()。 I Subscribe()与承担异常处理程序的重载,但从来没有击中所述异常处理程序中的断点(我没有想到这一点)。我订阅ThrownErrors,从来没有击中该异常处理程序的断点(我有点期待这一点)。如何捕获ReactiveCommand异常?

这里的示例代码:

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(); 
} 

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

回答

1

这是ReactiveUI当前版本中的一个错误 - 吞下创建'execute'observable时抛出的异常,并且ReactiveCommand的内部状态保持断开状态。这已在下一个版本中得到解决。

查看this github issue了解详情。

+0

整洁,谢谢你的信息。 –