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

  • 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!"));
}
}
主站蜘蛛池模板: 年辖:市辖区| 井研县| 拜泉县| 无棣县| 苍溪县| 军事| 勐海县| 黄浦区| 武山县| 耒阳市| 新民市| 阜康市| 敦煌市| 仙居县| 琼结县| 信丰县| 新乐市| 谢通门县| 平南县| 大埔区| 中卫市| 铁岭市| 宣化县| 砀山县| 新邵县| 宜良县| 印江| 乌兰察布市| 宜宾市| 买车| 马边| 华亭县| 南充市| 玉山县| 绥芬河市| 天气| 城口县| 乐业县| 克山县| 工布江达县| 贺州市|