When using ReactiveExtension Observer exceptions are not being caught by the onError action. Using the example code below instead of the exception being caught "An unhandled exception of type 'System.ApplicationException' occurred in System.Reactive.Core.dll" and the application terminates. The exception seems to bypass every try/catch in the calling stack.
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);
Am I missing how exceptions are supposed to handled by observables?
If I put a try catch in the onNext action then exception is caught and I can log it.
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);
What do I need to do to have the exception caught by the onError action?
Exceptions are only caught if they are raised in the observable. If they are raised in the observer then you have to catch them yourself.
This makes sense for a number of reasons:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With