how can RxJava v2 throw an exception to be handled imperatively? Is it possible?
@Throws(Throwable::class)
override fun onRun() {
//synchronous stream....
.subscribe(
{},
{ throw it } //I need this exception to be thrown by the wrapping `onRun` method
)
}
In RxJava1 I could do the following:
@Throws(Throwable::class)
override fun onRun() {
Observable.just("n/a")
.map {
throw RuntimeException()
}
.subscribe(
{},
{ throw it } //onRun() would catch the OnErrNotImplementedException, which I could unwrap
)
}
relevant documentation: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling
Obviously this is messy and not reactive - but it's a limitation of a third party library I'm using.
You can't throw upwards with subscribe(). You can, however, store the exception in a field or on the heap and once subscribe returns, read it:
var error = Throwable[1]
Observable.just("n/a")
.map {
throw RuntimeException()
}
.subscribe(
{},
{ error[0] = it }
)
assertNonNull(error[0])
That's what I'm gonna do, thanks!
Most helpful comment
You can't throw upwards with
subscribe(). You can, however, store the exception in a field or on the heap and oncesubscribereturns, read it: