Chr*_*unt 5 c# exception system.reactive
使用ReactiveExtension时,onError操作不会捕获异常.使用下面的示例代码而不是捕获的异常"在System.Reactive.Core.dll中发生未处理的类型'System.ApplicationException'异常"并且应用程序终止.异常似乎绕过了调用堆栈中的每个try/catch.
var source = Observable.Interval(TimeSpan.FromSeconds(seconds));
var observer = Observer.Create<long>(
l =>
{
//do something
throw new ApplicationException("test exception");
},
ex => Console.WriteLine(ex));
var subscription = source.Subscribe(observer);
Run Code Online (Sandbox Code Playgroud)
我错过了观察者应该如何处理异常?
如果我在onNext操作中放入try catch,则会捕获异常并且我可以记录它.
var source = Observable.Interval(TimeSpan.FromSeconds(seconds));
var observer = Observer.Create<long>(
l =>
{
try
{
//do something
throw new ApplicationException("test exception");
}
catch(Exception ex)
{
//exception can be caught here and logged
Console.WriteLine(ex);
}
},
ex => Console.WriteLine(ex));
var subscription = source.Subscribe(observer);
Run Code Online (Sandbox Code Playgroud)
如何通过onError操作捕获异常需要做什么?
只有在可观察量中出现异常时才会捕获异常.如果他们在观察者中长大,那么你必须亲自抓住他们.
这有道理,原因有很多: