This is an easy program to introduce the Reactive Framework. But I want to try the error handler, by modifying the program to be:
var cookiePieces = Observable.Range(1, 10);
cookiePieces.Subscribe(x =>
{
Console.WriteLine("{0}! {0} pieces of cookie!", x);
throw new Exception(); // newly added by myself
},
ex => Console.WriteLine("the exception message..."),
() => Console.WriteLine("Ah! Ah! Ah! Ah!"));
Console.ReadLine();
In this sample the follwing overload is used.
public static IDisposable Subscribe<TSource>(
this IObservable<TSource> source,
Action<TSource> onNext,
Action<Exception> onError,
Action onCompleted);
I hoped I would see the exception message printed, but the console application crashed. What is the reason?
The exception handler is used for exceptions created in the observable itself, not by the observer.
An easy way to provoke the exception handler is something like this:
Output: