IObservable.Do()
处理程序重载,OnError
但异常被传播到Subscribe()
调用,但如果OnError
在中指定Subscribe
- 异常不会传播给调用者 - 这是一个简单的例子:
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);
}