官术网_书友最值得收藏!

  • 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!"));
}
}
主站蜘蛛池模板: 哈尔滨市| 太原市| 佛冈县| 长阳| 邛崃市| 江津市| 稷山县| 河源市| 应用必备| 墨竹工卡县| 社旗县| 庆阳市| 锡林浩特市| 贺州市| 且末县| 武乡县| 遂溪县| 炉霍县| 邓州市| 府谷县| 合水县| 高雄市| 高尔夫| 子长县| 聂荣县| 晋宁县| 自治县| 南岸区| 乐清市| 嵊泗县| 甘肃省| 镇原县| 政和县| 南郑县| 义乌市| 兴海县| 监利县| 高邑县| 惠东县| 内丘县| 遵义县|