繁体   English   中英

如何用 observable 处理 async 方法抛出的异常?

[英]How to handle the exception thrown by the async method with observable?

我有一个 observable,我想用异步方法订阅这个 observable,但是每次异步方法抛出异常时,即使我将 catch 代码放在 observable 定义中,订阅也会立即处理。 下面的伪代码演示了这种情况:

[Fact]
public async Task Test()
{
    var observable = Observable.Create<int>(observer =>
    {
        try
        {
            Enumerable.Range(1, 10).ToList().ForEach(x =>
            {
                observer.OnNext(x);
            });
        }
        catch (Exception ex)
        {
           // get called after the exception is thrown 
            _testOutputHelper.WriteLine($"The exception is catch:{ex.ToString()}"); 
        }
        return Disposable.Create(() =>
        {
           // also get called after exception is thrown
            _testOutputHelper.WriteLine("Observable Dispose"); 
        });
    });

    Func<int, Task> handler = async (i) =>
     {
         // simulate the handler logic
         await Task.Delay(TimeSpan.FromSeconds(1));
         // throw the exception to test 
         throw new Exception($"{i}");
     };

    observable.Subscribe(x=>handler(x).Wait());

    await Task.Delay(TimeSpan.FromSeconds(10));
}

从上面的代码中,我不明白为什么即使捕获异常也会调用 dispose 委托(出于某种原因,我必须在可观察定义中处理异常),有没有办法防止订阅在异步方法抛出异常?

您的代码完全按照您的要求执行。

捕获异常的目的是让您的程序可以继续运行而不会突然停止。 这正是您的代码正在做的事情:异常被捕获,然后在catch块之后继续执行。

如果你想让它做其他事情,你有两个选择。

  1. 记录后重新抛出异常:
catch (Exception ex)
{
   // get called after the exception is thrown 
    _testOutputHelper.WriteLine($"The exception is catch:{ex.ToString()}");
    throw;
}

然后,任何称为Test()的代码都将负责捕获该异常(或不捕获)。

  1. 将 return 移到try块内,并在捕获到异常时返回其他内容:
try
{
    Enumerable.Range(1, 10).ToList().ForEach(x =>
    {
        observer.OnNext(x);
    });
    return Disposable.Create(() =>
    {
       // also get called after exception is thrown
        _testOutputHelper.WriteLine("Observable Dispose"); 
    });
}
catch (Exception ex)
{
   // get called after the exception is thrown 
    _testOutputHelper.WriteLine($"The exception is catch:{ex.ToString()}");
    
    return //something else
}

您可能会受益于阅读 Microsoft 的异常处理文档。

您的代码中发生的事情是您使用Observable.Create并使用以下代码填充 observable 的直接结果:

Enumerable.Range(1, 10).ToList().ForEach(x =>
{
    observer.OnNext(x);
});

Observable.Create使用当前线程创建 observable,因此Enumerable.Range(1, 10).ToList().ForEach立即在当前线程上执行,对OnNext的调用立即执行handler(x).Wait() .

但是,您会注意到,异常发生在传递给Subscribe的委托中。 内部有这样的代码:

catch (Exception exception)
{
    if (!autoDetachObserver.Fail(exception))
    {
        throw;
    }
    return autoDetachObserver;
}

这会在订阅中捕获异常,取消订阅 - 因此是"Observable Dispose"消息 - 然后重新抛出异常,这就是您的代码捕获它的地方。

现在,如果您想在 Rx 中正确执行此操作,则应避免使用Observable.Create 这是一种创建 observables 的诱人方式,但它会带来麻烦。

而是这样做:

public async Task Test()
{
    Func<int, Task> handler = async (i) =>
     {
         // simulate the handler logic
         await Task.Delay(TimeSpan.FromSeconds(1));
         // throw the exception to test 
         throw new Exception($"{i}");
     };
 
    await
        Observable
            .Range(1, 10)
            .SelectMany(i => Observable.FromAsync(() => handler(i)))
            .LastOrDefaultAsync();
}

但是,当然,我们要处理异常。 简单的方法是这样的:

public async Task Test()
{
    Func<int, Task> handler = async (i) =>
     {
         // simulate the handler logic
         await Task.Delay(TimeSpan.FromSeconds(1));
         // throw the exception to test 
         throw new Exception($"{i}");
     };
 
    await
        Observable
            .Range(1, 10)
            .SelectMany(i =>
                Observable
                    .FromAsync(() => handler(i))
                    .Catch<Unit, Exception>(ex =>
                    {
                        Console.WriteLine($"The exception is catch:{ex.ToString()}");
                        return Observable.Empty<Unit>();
                    }))
            .LastOrDefaultAsync();
}

现在输出 10 个异常错误并正常完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM