I'm learning RxScala and came to this very synthetic snippet. I'm trying to handle exception in onError block:
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")
)
}
In case of exception I expect something like:
Failed: Something went wrong during long operation
But what I get is:
.........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)
What am I missing? Should I 'manually' call onError at observer? Appreciate for any help.