在不停止序列的情况下处理无效扩展中的异常 [英] Handling Exceptions in Reactive Extensions without stopping sequence

查看:155
本文介绍了在不停止序列的情况下处理无效扩展中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么RX有以下语法 OnNext *(OnError | OnCompleted)?而不是(OnNext | OnError)* OnCompleted ?这从实现的角度来看是非常清楚的(也有这个通用语义与 IEnumerable yield ),但我猜不同于真实生活状况在现实生活中,生产者产生混合数据和异常流(异常不会破坏生产者)。

Why RX has the following grammar OnNext* (OnError|OnCompleted)? instead of (OnNext|OnError)* OnCompleted? This is quite clear from implementation perspective(also this has common semantics with IEnumerable and yield) but I guess differs from real life situation. In real life - producers generate mixed stream of data and exceptions (and exceptions doesn't break producer).

问题:
如果我理解正确,唯一可能的解决方案是使可观察的返回复杂数据结构与初始数据和生成的异常相结合( Observable.Timestamp() .TimeInterval()有类似的概念)还是有其他选项?

The question: If I understood correctly the only possible solution is to make observable return complex data structure combined from initial data and produced exceptions(Observable.Timestamp() and .TimeInterval() has similar concept) or are there any other options?

目前我来到以下解决方案:
内部可观察的生产者我手动处理exeptions,并将其传递到以下数据结构中是我可观察的结果

At the moment I came to the following solution: Inside observable producer I manually handle exeptions and just pass them to the following data structure which is the result of my observable

public class ValueOrException<T>
{
    private readonly Exception ex;
    private readonly T value;

    public ValueOrException(T value, Exception ex)
    {
        this.value = value;
        this.ex = ex;
    }

    public ValueOrException(T value)
    {
        this.value = value;
    }

    public T Value
    {
        get { return this.value; }
    }

    public Exception Ex
    {
        get { return this.ex; }
    }
}


推荐答案

如果消费者知道 异常 是否预期,那么我说这个例外是不正确的。

If the consumer is the one that knows if the Exception is expected or not, then I say that throwing exceptions is incorrect here.

您本质上是要传达状态或逻辑,哪些异常不是要做的。例外是为了使系统(或至少是当前的操作)停止,因为发生了代码本身并不知道如何从中恢复的事情。

You're essentially trying to convey state or logic, which exceptions are not meant to do. Exceptions are meant to bring the system (or at the very least, the current operation) to a grinding halt because something has happened which the code doesn't inherently know how to recover from.

在这种情况下,只要您的业务逻辑/状态异常部分,但这并不意味着你可以扔他们您需要将它们下传到您的消费者,然后让您的消费者处理它们。

In this case, it just so happens that an Exception is part of your business logic/state, but that doesn't mean you can throw them. You need to pass them downstream to your consumer and then have your consumer process them.

A 元组 将在这里工作,但是类型(以及属性)缺乏特定性通常是凌乱的,所以建议您创建一个专用类型来传达您的操作结果,并通过 IObservable< T>

A Tuple<T, Exception> would work in a pinch here, but the lack of specificity around the type (as well as the properties) is usually messy, so I'd recommend creating a dedicated type to convey the results of your operation and is exposed through the IObservable<T>.

这篇关于在不停止序列的情况下处理无效扩展中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆