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

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!"));
}
}
主站蜘蛛池模板: 澄江县| 南昌市| 霍邱县| 昌江| 诏安县| 大埔县| 香河县| 苏尼特右旗| 湘阴县| 东城区| 乐清市| 泰宁县| 库伦旗| 襄汾县| 怀宁县| 睢宁县| 滦平县| 西华县| 罗平县| 新化县| 玉环县| 高邑县| 琼结县| 新乡市| 成安县| 广南县| 永新县| 尼勒克县| 永年县| 大姚县| 平潭县| 格尔木市| 祁连县| 旌德县| 柳州市| 射阳县| 左权县| 武威市| 长岭县| 苗栗县| 绍兴市|