只是对为什么流程没有到达 catch 块感到困惑。我确实明白,每当发生异常时OnError
都会调用方法,但是在调用时不应该发生异常ToObservable
吗?只有在处理第三个数字(0)时,才会发生异常。我完全糊涂了。
static void Main()
{
try
{
var numbers = from number in
new int[] { 1, 2, 0, 3 }
select 10 / number;
var observable = numbers.ToObservable();
observable.Subscribe(OnNext, OnError, OnComplete);
Console.ReadKey();
}
catch (Exception exc)
{
Console.WriteLine("Program terminated with the following message - {0}", exc.Message);
}
}
private static void OnNext(int i)
{
Console.WriteLine("Output : " + i);
}
private static void OnError(Exception exc)
{
Console.WriteLine("In oops : {0}", exc.Message);
}
private static void OnComplete()
{
Console.WriteLine("In done");
}