我正在學習RxScala並來到這個非常合成的片段。我試圖處理例外的onError塊:可觀察的異常處理
def doLongOperation():String = {
(1 to 10).foreach {
_ =>
Thread.sleep(100)
print(".")
}
println()
if (System.currentTimeMillis() % 2 == 0) {
throw new RuntimeException("Something went wrong during long operation")
}
s"OK"
}
def main(args: Array[String]) {
println("Changing status: -> doing task1")
Observable.just(
doLongOperation()
).subscribe(
str => println("Changing status: doing task1 -> doing task2"),
throwable => println(s"Failed: ${throwable.getMessage}"), //never get here
() => println("Completed part")
)
}
在例外的情況下,我希望是這樣的:
Failed: Something went wrong during long operation
但我得到的是:
.........Exception in thread "main" java.lang.RuntimeException: Something went wrong during long operation
at stats.STest$.doLongOperation(STest.scala:20)
at stats.STest$.main(STest.scala:49)
at stats.STest.main(STest.scala)
我是什麼失蹤?我應該在觀察者處「手動」調用onError嗎?感謝您的幫助。
非常感謝!現在事情變得更加清晰了 – Nyavro