- 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!"));
}
}
推薦閱讀
- 構建移動網(wǎng)站與APP:HTML 5移動開發(fā)入門與實戰(zhàn)(跨平臺移動開發(fā)叢書)
- RTC程序設計:實時音視頻權威指南
- Python高級機器學習
- Redis Essentials
- Hands-On Reinforcement Learning with Python
- C程序設計實踐教程
- Haskell Data Analysis Cookbook
- Programming with CodeIgniterMVC
- Raspberry Pi Robotic Blueprints
- Illustrator CS6設計與應用任務教程
- 交互式程序設計(第2版)
- Mastering Magento Theme Design
- 開源網(wǎng)絡地圖可視化:基于Leaflet的在線地圖開發(fā)
- MATLAB從入門到精通
- 自己動手做智能產品:嵌入式JavaScript實現(xiàn)