如何检查 RX Final 方法内的错误?
以下代码在迭代器中引发错误(出于测试目的)。 正如您所看到的,订阅可以使用 Subscribe 方法的 onError 参数轻松地获取异常,但我似乎无法弄清楚finally 方法如何测试/获取异常(因此修改以下内容) -up 行为取决于结果)
static void Main(string[] args)
{
// pseudo sequence
var sequence = Observable.GenerateWithTime(
(double)0,
i => i < 10,
i => i += 0.5,
i => {
if(i < 6) return i;
throw new InvalidOperationException();
},
i => TimeSpan.FromSeconds(1)
).Timestamp().Finally(() => Console.WriteLine("finally!"));
using (
sequence.Subscribe(x =>
{
Console.WriteLine(x);
},
err => Console.WriteLine(err.ToString()) // Write error to console
)
)
{
Console.ReadLine();
}
Console.ReadLine();
}
注意:此代码仅用于演示目的,但它确实有效:)
更新
对上面的演示进行以下修改允许实现所需的行为 - 感谢 James Hay。
static void Main(string[] args)
{
bool errored = false;
var sequence = Observable.GenerateWithTime(
(double)0,
i => i < 10,
i => i += 0.5,
i =>
{
if (i < 6) return i;
throw new InvalidOperationException();
},
i => TimeSpan.FromSeconds(1)
).Catch(
new Func<Exception, IObservable<double>>(x => {
errored = true;
return Observable.Return((double)0);
})
).Timestamp().Finally(() => Console.WriteLine("finally!" + (errored?"(errored)":"")));
using (
sequence.Subscribe(objects =>
{
Console.WriteLine(objects);
},
err => Console.WriteLine(err.ToString())
)
)
{
Console.ReadLine();
}
Console.ReadLine();
}
The following code throws an error in the iterator (on purpose for testing).
As you can see, the subscription easily gets hold of the exception using the onError parameter of the Subscribe method, but what I can't seem to work out is how the finally method could test/get hold of the exception (and therefore modify follow-up behaviour depending on the outcome)
static void Main(string[] args)
{
// pseudo sequence
var sequence = Observable.GenerateWithTime(
(double)0,
i => i < 10,
i => i += 0.5,
i => {
if(i < 6) return i;
throw new InvalidOperationException();
},
i => TimeSpan.FromSeconds(1)
).Timestamp().Finally(() => Console.WriteLine("finally!"));
using (
sequence.Subscribe(x =>
{
Console.WriteLine(x);
},
err => Console.WriteLine(err.ToString()) // Write error to console
)
)
{
Console.ReadLine();
}
Console.ReadLine();
}
Note: this code is for demo purposes only, but it does work :)
Update
The following modifications to the demo above allowed the desired behaviour - thanks to James Hay.
static void Main(string[] args)
{
bool errored = false;
var sequence = Observable.GenerateWithTime(
(double)0,
i => i < 10,
i => i += 0.5,
i =>
{
if (i < 6) return i;
throw new InvalidOperationException();
},
i => TimeSpan.FromSeconds(1)
).Catch(
new Func<Exception, IObservable<double>>(x => {
errored = true;
return Observable.Return((double)0);
})
).Timestamp().Finally(() => Console.WriteLine("finally!" + (errored?"(errored)":"")));
using (
sequence.Subscribe(objects =>
{
Console.WriteLine(objects);
},
err => Console.WriteLine(err.ToString())
)
)
{
Console.ReadLine();
}
Console.ReadLine();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Finally
将在发生错误或序列完成后发生。因此,当执行命中Finally 运算符时,可能不会出现错误。如果您希望在抛出错误时在序列中发生某些事情,请查看
Catch
运算符。因此,您可以添加副作用并重新抛出错误:
或者您可以只处理错误并以某种方式继续执行序列:
Finally
will happen after an error occurs OR the sequence completes. So there may not be an error when execution hits the Finally operator.If you want something to happen within the sequence when the error is thrown look into the
Catch
operator.So you could add a side effect and re-throw the error:
Or you could just handle the error and continue on through the sequence in some way: