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

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!"));
}
}
主站蜘蛛池模板: 施甸县| 周宁县| 晴隆县| 富顺县| 梁平县| 江都市| 翁牛特旗| 板桥市| 拉孜县| 吉水县| 邢台县| 莆田市| 卢龙县| 义马市| 双鸭山市| 广河县| 钟祥市| 佛坪县| 沙湾县| 洞头县| 昭觉县| 图们市| 兴安县| 南华县| 汽车| 勃利县| 喜德县| 永寿县| 阿拉尔市| 湟中县| 佛冈县| 白河县| 小金县| 三门峡市| 永州市| 偃师市| 齐河县| 平度市| 安庆市| 铜山县| 桃园市|