- 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!"));
}
}
推薦閱讀
- MySQL數(shù)據(jù)庫管理實戰(zhàn)
- ASP.NET Core 5.0開發(fā)入門與實戰(zhàn)
- PHP程序設計(慕課版)
- 深入實踐Spring Boot
- 少年輕松趣編程:用Scratch創(chuàng)作自己的小游戲
- Django Design Patterns and Best Practices
- 小程序,巧運營:微信小程序運營招式大全
- Android Native Development Kit Cookbook
- KnockoutJS Starter
- Gradle for Android
- 數(shù)據(jù)結(jié)構(gòu)習題解析與實驗指導
- Python Data Structures and Algorithms
- OpenCV with Python By Example
- Python Interviews
- Mobile Forensics:Advanced Investigative Strategies