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

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!"));
}
}
主站蜘蛛池模板: 普格县| 紫阳县| 新巴尔虎右旗| 北票市| 沈阳市| 绥棱县| 纳雍县| 博客| 贺州市| 大理市| 嘉禾县| 曲阳县| 吉安市| 邵武市| 绍兴县| 阿城市| 门源| 巴塘县| 新龙县| 武汉市| 九江县| 延津县| 额济纳旗| 察雅县| 长泰县| 文山县| 滦南县| 大余县| 滨州市| 黔西| 横山县| 视频| 微博| 荃湾区| 莎车县| 广州市| 隆安县| 长阳| 周宁县| 贺州市| 城固县|