1
IObservable.Do()
已經過載與OnError
處理程序,但異常傳播到Subscribe()
呼叫,但如果Subscribe
指定OnError
- 異常不會傳播給調用者 - 在這裏是簡單的例子:在DO()爲什麼的OnError行爲)和訂閱(在.NET RX不同
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);
}
感謝您的澄清。你可以評論這個問題http://stackoverflow.com/q/13310865/296494 PLZ,如果我理解正確'.Do()'鏈不是最好的選擇那裏。 –