捕获可能会从Subscription OnNext Action抛出的异常 [英] Catching exceptions which may be thrown from a Subscription OnNext Action

查看:395
本文介绍了捕获可能会从Subscription OnNext Action抛出的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Rx.NET有点新鲜。是否有可能捕获任何订阅者可能抛出的异常?采取以下...

  handler.FooStream.Subscribe(
_ => throw new Exception(Bar ),
_ => {});

目前,我正在使用以下实例捕获每个订阅。其实现只是使用一个ManualResetEvent来唤醒等待线程。

  public interface IExceptionCatcher 
{
动作< T> Exec< T>(Action< T>动作);
}

并使用它像...

  handler.FooStream.Subscribe(
_exceptionCatcher.Exec&Foo>(_ => throw new Exception(Bar))// //令人失望的是,这种通用类型不能被推断
_ => {});

我觉得有一些更好的方法。 Rx.NET中的所有错误处理能力专门用于处理错误可观察的问题吗?



编辑:根据请求,我的实现是 https://gist.github.com/1409829 (界面和实现在prod代码中分为不同的程序集)。欢迎反馈。这可能看起来很傻,但是我正在使用城堡温莎来管理许多不同的Rx用户。这个异常捕获器就像这样向容器注册

  windsorContainer.Register(Component.For< IExceptionCatcher>() )); 

然后它将像这样使用 observable 是IObservable的实例:

  var exceptionCatcher = 
new ExceptionCatcher(e =>
{
Logger.FatalException(
异常被捕获,关闭,e);
//处理非托管资源
},false);


/ *
*通常,下面的代码存在于由IoC容器管理的某个类中。
*'catcher'将由容器提供。
* /
observable / *做一些过滤,选择,分组等* /
.SubscribeWithExceptionCatching(processItems,catcher);


解决方案

内置的可观察操作符不会做你所做的默认情况下要求(非常类似事件),但您可以制作一个扩展方法来执行此操作。

  public static IObservable< ; T> IgnoreObserverExceptions< T,TException>(
此IObservable< T>源
)其中TException:异常
{
return Observable.CreateWithDisposable< T>(
o => source.Subscribe(
v => {try {o.OnNext(v);}
catch(TException){}
},
ex => o.OnError ex),
()=> o.OnCompleted()
));
}

然后,任何observable都可以通过此方法包装,以获得您所描述的行为。


I'm somewhat new to Rx.NET. Is it possible to catch an exception which may be thrown by any of the subscribers? Take the following...

handler.FooStream.Subscribe(
            _ => throw new Exception("Bar"),
            _ => { });

Currently I'm catching on a per subscription basis with an instance of the following. The implementation of which just uses a ManualResetEvent to wake up a waiting thread.

public interface IExceptionCatcher
{
    Action<T> Exec<T>(Action<T> action);
}

and using it like so...

handler.FooStream.Subscribe(
            _exceptionCatcher.Exec<Foo>(_ => throw new Exception("Bar")), //It's disappointing that this generic type can't be inferred
            _ => { });

I feel like there must be some better way. Are all of the error handling capabilities in Rx.NET specifically for dealing with errors observables?

EDIT: Per request, my implementation is https://gist.github.com/1409829 (interface and implementation separated into different assemblies in prod code). Feedback is welcome. This may seem silly, but I'm using castle windsor to manage many different Rx subscribers. This exception catcher is registered with the container like this

windsorContainer.Register(Component.For<IExceptionCatcher>().Instance(catcher));

It would then be used like this where observable is instance of IObservable:

var exceptionCatcher =
    new ExceptionCatcher(e =>
                                {
                                    Logger.FatalException(
                                        "Exception caught, shutting down.", e);
                                    // Deal with unmanaged resources here
                                }, false);


/* 
 * Normally the code below exists in some class managed by an IoC container.
 * 'catcher' would be provided by the container.
 */
observable /* do some filtering, selecting, grouping etc */
    .SubscribeWithExceptionCatching(processItems, catcher);

解决方案

The built-in Observable operators do not do what you are asking for by default (much like events), but you could make an extension method that would do this.

public static IObservable<T> IgnoreObserverExceptions<T, TException>(
                                this IObservable<T> source
                               ) where TException : Exception
{
    return Observable.CreateWithDisposable<T>(
        o => source.Subscribe(
            v => { try { o.OnNext(v); }
                   catch (TException) { }
            },
            ex => o.OnError(ex),
            () => o.OnCompleted()
            ));
}

Then any observable could be wrapped by this method to get the behavior you described.

这篇关于捕获可能会从Subscription OnNext Action抛出的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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