IObservable.Do()
has overload with OnError
handler but exception is propagated to Subscribe()
call, but if OnError
is specified in Subscribe
- exception is not propagated to the caller - here is the simple example:
public static void Main()
{
OnErrorInDo(); // throws
OnErrorInSubscribe(); // doesn't throw
}
public void OnErrorInDo()
{
var observableThatThrows = GetEnumerableThatThrows().ToObservable();
var res = observableThatThrows.Do(i => Console.Write("{0}, ", i), LogEx).Subscribe();
}
public void OnErrorInSubscribe()
{
var observableThatThrows = GetEnumerableThatThrows().ToObservable();
var res = observableThatThrows.Do(i => Console.Write("{0}, ", i), LogEx)
.Subscribe(i=>{}, LogEx);
}
public IEnumerable<int> GetEnumerableThatThrows()
{
foreach (var i in Enumerable.Range(0,10))
{
if (i != 5)
yield return i;
else
throw new Exception("ex in enumerable");
}
}
public void LogEx(Exception ex)
{
Console.WriteLine("Ex message:" + ex.Message);
}