Paul Woitaschek
12/30/2019, 9:53 AMIllegalStateException
because a CancellationException
is a subclass and it puts your application into an unexpected state if you catch it. Just found out the hard wayDico
12/30/2019, 12:39 PMPaul Woitaschek
12/30/2019, 12:41 PMkotlinx.coroutines.CancellationException
is a typealias of java.util.concurrent.CancellationException
and that extends java.lang.IllegalStateException
CancellationException
😕error
also throw IllegalStateExeptionAdam Powell
12/30/2019, 3:53 PMIllegalStateException
? Making assumptions about how to recover from one vs. Exception
or Throwable
seems quite brittle since you don't know what threw it or why without further info, like a more specific exception typeDico
12/30/2019, 3:54 PMPaul Woitaschek
12/30/2019, 3:55 PMAdam Powell
12/30/2019, 4:00 PMThrowable
Dico
12/30/2019, 4:01 PMPaul Woitaschek
12/30/2019, 4:08 PMAdam Powell
12/30/2019, 4:12 PMIllegalStateException
. Job cancellation exists at the library layer, not a language layer. The mechanism for stack unwinding in such circumstances is to throw. catch (t: Throwable)
has the same gotcha and requires the same consideration, but given the rest of the advantages of the design imo it's worth the tradeoffsPaul Woitaschek
12/30/2019, 4:14 PMAdam Powell
12/30/2019, 4:15 PMPaul Woitaschek
12/30/2019, 4:19 PMtry {
thirdPartyApi.doSomethingThatCanThrow()
} catch (e: IllegalStateException) {
e.printStackTrace()
}
try {
thirdPartyApi.doSomethingThatCanThrow()
delay(100)
} catch (e: IllegalStateException) {
e.printStackTrace()
}
elizarov
12/30/2019, 9:11 PMorafaaraujo
01/21/2020, 11:52 AMNPE
I can't get it with .catch { e -> emit(Result.Error(Exception(e))) }
?
Tthis is what is happening here... I'm receiving an NPE and my catch
is getting it, but it's not emitting my Result.Error
. The app is still crashing 😞