Mark
01/13/2021, 2:31 AMCancellationException
? For example, any inline (not suspending) higher order function could innocently be catching Exception
and converting to some sealed class. It knows nothing about coroutines and so would have no reason to give special treatment to CancellationException
.
fun main() {
runBlocking {
val job = launch {
test {
delay(1000L)
println("finished long delay")
}
println("finished test")
}
delay(100L)
job.cancel()
}
}
inline fun test(block: () -> Unit) {
try {
block()
} catch (e: Exception) {
println(e.toString())
}
}
// kotlinx.coroutines.JobCancellationException: StandaloneCoroutine was cancelled; job="coroutine#2":StandaloneCoroutine{Cancelling}@de0a01f
// finished test
muliyul
01/13/2021, 2:47 AMinline fun safeCoTry(block: () -> Unit) {
try {
block()
} catch (e: Exception) {
if(e is CancellationException) // propogate
throw e
println(e.toString())
}
}
Mark
01/13/2021, 2:48 AMmuliyul
01/13/2021, 2:50 AMMark
01/13/2021, 2:51 AMmuliyul
01/13/2021, 2:53 AMMark
01/13/2021, 2:58 AMException
thrown from lambda argument.ephemient
01/13/2021, 3:02 AMMark
01/13/2021, 3:07 AMCancelletionException
seems like a very different type of exception that perhaps deserves its own category, much like how Error
is treated differently.ephemient
01/13/2021, 5:09 AMMark
01/13/2021, 5:14 AMAdam Powell
01/13/2021, 5:35 PMIllegalStateException
in general (which CancellationException
extends) tends to indicate something that shouldn't wrap/log and continue.