gpt4 book ai didi

c# - 如何捕获 ReactiveCommand 异常?

转载 作者:太空宇宙 更新时间:2023-11-03 12:49:18 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com