ReactiveX 学习笔记(8)错误处理和 To 操作符

时间:2023-03-08 16:23:55

Error Handling Operators

Operators to Convert Observables

本文的主题为对 Observable 进行错误处理的操作符以及转换 Observable 类型的操作符。

这里的 Observable 实质上是可观察的数据流。

RxJava操作符(五)Error Handling

公共代码

  • RxNET
public static void Dump<T>(this IObservable<T> source, string name)
{
source.Subscribe(
i => Console.WriteLine("{0}-->{1}", name, i),
ex => Console.WriteLine("{0} failed-->{1}", name, ex.Message),
() => Console.WriteLine("{0} completed", name));
}
  • RxJava
fun <T> Observable<T>.dump() =
this.subscribe({ println("onNext: $it") },
{ e -> println("onError: ${e.javaClass.name}: ${e.message}") },
{ println("onComplete") })

Catch / OnErrorResumeNext

ReactiveX - Catch operator

Reactive Extensions再入門 その11「Catchメソッド」

Reactive Extensions再入門 その34「ダメなら次の人!を実現するOnErrorResumeNextメソッド」

当源数据流出错抛出异常时,Catch 能捕捉该异常并从中恢复。相当于 catch 语句。

不指定异常类型时,Catch 能捕捉所有类型的异常。

指定异常类型时,Catch 只能捕捉指定类型的异常。

OnErrorResumeNext 与 VB 的同名语句的作用相同。使用该操作符的话,一个数据流无论出错还是正常结束都会转到后一个指定数据流。

ReactiveX 学习笔记(8)错误处理和 To 操作符

ReactiveX 学习笔记(8)错误处理和 To 操作符

  • RxNET
var source = new Subject<int>();
var result = source.Catch(Observable.Empty<int>());
result.Dump("Catch");
source.OnNext(1);
source.OnNext(2);
source.OnError(new Exception("Fail!"));
/*
Catch-->1
Catch-->2
Catch completed
*/
var source = new Subject<int>();
var result = source.Catch<int, TimeoutException>(tx => Observable.Return(-1));
result.Dump("Catch");
source.OnNext(1);
source.OnNext(2);
source.OnError(new TimeoutException());
/*
Catch-->1
Catch-->2
Catch-->-1
Catch completed
*/
var source = new Subject<int>();
var result = source.Catch<int, TimeoutException>(tx => Observable.Return(-1));
result.Dump("Catch");
source.OnNext(1);
source.OnNext(2);
source.OnError(new ArgumentException("Fail!"));
/*
Catch-->1
Catch-->2
Catch failed-->Fail!
*/
Observable
.Throw<string>(new Exception())
.OnErrorResumeNext(Observable.Return("OK"))
.Subscribe(
s => Console.WriteLine("OnNext: {0}", s),
ex => Console.WriteLine("OnError: {0}", ex),
() => Console.WriteLine("OnCompleted"));
new[] { "NG", "Error", "Abort", "OK" }
.Select((s, i) => new { index = i, value = s })
.Select(s => s.value != "OK" ?
Observable.Throw<string>(new Exception(s.ToString())) :
Observable.Return(s.ToString()))
.OnErrorResumeNext()
.Subscribe(
s => Console.WriteLine("OnNext: {0}", s),
ex => Console.WriteLine("OnError: {0}", ex),
() => Console.WriteLine("OnCompleted"));
new[] { "NG", "OK", "Abort", "Error" }
.Select((s, i) => new { index = i, value = s })
.Select(s => s.value != "OK" ?
Observable.Throw<string>(new Exception(s.ToString())) :
Observable.Return(s.ToString()))
.OnErrorResumeNext()
.Subscribe(
s => Console.WriteLine("OnNext: {0}", s),
ex => Console.WriteLine("OnError: {0}", ex),
() => Console.WriteLine("OnCompleted"));
new[] { "NG", "Exception", "Abort", "Error" }
.Select((s, i) => new { index = i, value = s })
.Select(s => s.value != "OK" ?
Observable.Throw<string>(new Exception(s.ToString())) :
Observable.Return(s.ToString()))
.OnErrorResumeNext()
.Subscribe(
s => Console.WriteLine("OnNext: {0}", s),
ex => Console.WriteLine("OnError: {0}", ex),
() => Console.WriteLine("OnCompleted"));
/*
OnNext: OK
OnCompleted
OnNext: { index = 3, value = OK }
OnCompleted
OnNext: { index = 1, value = OK }
OnCompleted
OnCompleted
*/
  • RxJava

ReactiveX 学习笔记(8)错误处理和 To 操作符

ReactiveX 学习笔记(8)错误处理和 To 操作符

ReactiveX 学习笔记(8)错误处理和 To 操作符

val values = Observable.create<String> { o ->
o.onNext("Rx")
o.onNext("is")
o.onError(Exception("adjective unknown"))
}
values
.onErrorReturn { e -> "Error: " + e.message }
.dump()
/*
onNext: Rx
onNext: is
onNext: Error: adjective unknown
onComplete
*/
val values = Observable.create<Int> { o ->
o.onNext(1)
o.onNext(2)
o.onError(Exception("Oops"))
}
values
.onErrorResumeNext(Observable.just(Integer.MAX_VALUE))
.dump()
/*
onNext: 1
onNext: 2
onNext: 2147483647
onComplete
*/
val values = Observable.create<Int> { o ->
o.onNext(1)
o.onNext(2)
o.onError(Exception("Oops"))
}
values
.onErrorResumeNext( Function { e -> Observable.error(UnsupportedOperationException(e)) } )
.dump()
/*
onNext: 1
onNext: 2
onError: java.lang.UnsupportedOperationException: java.lang.Exception: Oops: java.lang.Exception: Oops
*/
val values = Observable.create<String> { o ->
o.onNext("Rx")
o.onNext("is")
// o.onError(Throwable()) // this won't be caught
o.onError(Exception()) // this will be caught
}
values
.onExceptionResumeNext(Observable.just("hard"))
.dump()
/*
onNext: Rx
onNext: is
onNext: hard
onComplete
*/
val values = Observable.create<String> { o ->
o.onNext("Rx")
o.onNext("is")
o.onError(object : Throwable() {
}) // this won't be caught
}
values
.onExceptionResumeNext(Observable.just("hard"))
.dump()
/*
onNext: Rx
onNext: is
onNext: hard
onComplete
*/
  • RxSwift
let disposeBag = DisposeBag()
let sequenceThatFails = PublishSubject<String>()
sequenceThatFails
.catchErrorJustReturn("