在处理无扩展的异常没有停止序列 [英] Handling Exceptions in Reactive Extensions without stopping sequence

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

问题描述

为什么RX具有以下语法 OnNext *(的OnError | OnCompleted)?而不是(OnNext |的OnError)* OnCompleted ?这是从实现的角度来看很清楚(也是这与的IEnumerable 收益率通用语义),但我猜不同于现实生活状况。在现实生活中 - 生产商产生的数据和异常的混合流(和例外情况不破制片人)。

问题: 如果我理解正确的唯一可能的解决方案是从最初的数据和产生异常(联合观测的返回复杂的数据结构 Observable.Timestamp()。 TimeInterval所()有类似的概念),还是有什么其他选择?


目前,我来到了以下解决方案: 内部观察的制片人我手动处理产生的异常,只是将它们传递到下面的数据结构,这是我观察到的结果

 公共类ValueOrException< T>
{
    私人只读异常前;
    私人只读的T值;

    公共ValueOrException(吨价,异常前)
    {
        THIS.VALUE =价值;
        this.ex =前;
    }

    公共ValueOrException(T值)
    {
        THIS.VALUE =价值;
    }

    公众吨价
    {
        {返回THIS.VALUE; }
    }

    公共异常前
    {
        {返回this.ex; }
    }
}
 

解决方案

如果消费者是一个知道如果异常预计与否的一个,那么我说, 投掷的例外是不正确位置。

你基本上要表达的状态或逻辑,哪些异常并不意味着做。例外情况是为了使系统(或至少是,当前操作)嘎然而止,因为事情已经发生该code本身不知道如何才能恢复。

在这种情况下,它只是恰巧,一个异常部分的的业务逻辑/状态,但是,这并不意味着你可以把他们。你需要向下游传递到您的消费者,然后让消费者对它们进行处理。

A 元组LT; T,异常> 将在这里工作紧要关头,但周围的类型缺乏特异性(以及属性)通常是凌乱的,所以我建议建立一个专门的类型来传达你的操作的结果,并通过公开的的IObservable< T>

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).

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?


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 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天全站免登陆