- Learning RxJava
- Thomas Nield
- 128字
- 2021-07-02 22:22:54
Observable.error()
This too is something you likely will only do with testing, but you can create an Observable that immediately calls onError() with a specified exception:
import io.reactivex.Observable;
public class Launcher {
public static void main(String[] args) {
Observable.error(new Exception("Crash and burn!"))
.subscribe(i -> System.out.println("RECEIVED: " + i),
Throwable::printStackTrace,
() -> System.out.println("Done!"));
}
}
The output is as follows:
java.lang.Exception: Crash and burn!
at Launcher.lambda$main$0(Launcher.java:7)
at io.reactivex.internal.operators.observable.
ObservableError.subscribeActual(ObservableError.java:32)
at io.reactivex.Observable.subscribe(Observable.java:10514)
at io.reactivex.Observable.subscribe(Observable.java:10500)
...
You can also provide the exception through a lambda so that it is created from scratch and separate exception instances are provided to each Observer:
import io.reactivex.Observable;
public class Launcher {
public static void main(String[] args) {
Observable.error(() -> new Exception("Crash and burn!"))
.subscribe(i -> System.out.println("RECEIVED: " + i),
Throwable::printStackTrace,
() -> System.out.println("Done!"));
}
}
推薦閱讀
- Node.js+Webpack開發(fā)實(shí)戰(zhàn)
- 數(shù)據(jù)結(jié)構(gòu)和算法基礎(chǔ)(Java語言實(shí)現(xiàn))
- Python 深度學(xué)習(xí)
- Learning ArcGIS Pro 2
- Python自動(dòng)化運(yùn)維快速入門
- 實(shí)戰(zhàn)Java程序設(shè)計(jì)
- Java EE 7 Development with NetBeans 8
- 自然語言處理Python進(jìn)階
- HTML5 APP開發(fā)從入門到精通(微課精編版)
- Android應(yīng)用案例開發(fā)大全(第二版)
- Scala for Machine Learning(Second Edition)
- BeagleBone Robotic Projects(Second Edition)
- Backbone.js Testing
- Java Hibernate Cookbook
- C++服務(wù)器開發(fā)精髓