My application terminates when an error is thrown in OnNext
by an observer when I use ObserveOn(Scheduler.ThreadPool)
. The only way I have found to deal with this is by using a custom extension method below (apart from making sure OnNext never throws an exception). And then making sure that each ObserveOn
is followed by an ExceptionToError
.
public static IObservable<T> ExceptionToError<T>(this IObservable<T> source) {
var sub = new Subject<T>();
source.Subscribe(i => {
try {
sub.OnNext(i);
} catch (Exception err) {
sub.OnError(err);
}
}
, e => sub.OnError(e), () => sub.OnCompleted());
return sub;
}
However, this does not feel right. Is there a better way to deal with this?
Example
This program crashes because of uncaught exception.
class Program {
static void Main(string[] args) {
try {
var xs = new Subject<int>();
xs.ObserveOn(Scheduler.ThreadPool).Subscribe(x => {
Console.WriteLine(x);
if (x % 5 == 0) {
throw new System.Exception("Bang!");
}
}, ex => Console.WriteLine("Caught:" + ex.Message)); // <- not reached
xs.OnNext(1);
xs.OnNext(2);
xs.OnNext(3);
xs.OnNext(4);
xs.OnNext(5);
} catch (Exception e) {
Console.WriteLine("Caught : " + e.Message); // <- also not reached
} finally {
Console.ReadKey();
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…