如何检查 RX Final 方法内的错误?

发布于 2024-10-30 04:33:50 字数 2092 浏览 0 评论 0原文

以下代码在迭代器中引发错误(出于测试目的)。 正如您所看到的,订阅可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香橙ぽ 2024-11-06 04:33:50

Finally 将在发生错误序列完成后发生。因此,当执行命中Finally 运算符时,可能不会出现错误。

如果您希望在抛出错误时在序列中发生某些事情,请查看 Catch 运算符。

因此,您可以添加副作用并重新抛出错误:

.Catch(ex =>
{
   //Add a side effect here

   return Observable.Throw(ex); 
})

或者您可以只处理错误并以某种方式继续执行序列:

.Catch(ex => Observable.Return("Some continue on error observable")));

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:

.Catch(ex =>
{
   //Add a side effect here

   return Observable.Throw(ex); 
})

Or you could just handle the error and continue on through the sequence in some way:

.Catch(ex => Observable.Return("Some continue on error observable")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文