- 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!"));
}
}
推薦閱讀
- The Complete Rust Programming Reference Guide
- Oracle從新手到高手
- 程序員面試算法寶典
- Java開發入行真功夫
- Mathematica Data Analysis
- Node.js全程實例
- Unity 2018 Shaders and Effects Cookbook
- Babylon.js Essentials
- Kotlin開發教程(全2冊)
- App Inventor少兒趣味編程動手做
- Photoshop智能手機APP界面設計
- Python Social Media Analytics
- 零基礎學Java(第5版)
- SaaS攻略:入門、實戰與進階
- Magento 2 Developer's Guide