Why does launch swallow exceptions in kotlin coroutines?
Compare your test with the following one that does not use any coroutines, but starts a new thread instead:
@Test
fun why_does_this_test_pass() {
val job = thread { // <-- NOTE: Changed here
throw IllegalStateException("why does this exception not fail the test?")
}
// NOTE: No need for runBlocking any more
job.join() // ensures exception is thrown before test function completes
}
What happens here? Just like the test with launch
, this test passes if you run it, but the exception gets printed on the console.
So, using launch
to start a new coroutine is very much like using thread
to start a new thread. If it fails, the error gets handled by uncaught exception handler in thread
and by CoroutineExceptionHandler
(see it in the docs) by launch
. Exceptions in launch are not swallowed, but are handled by the coroutine exception handler.
If you want exception to propagate to the test, you shall replace launch
with async
and replace join
with await
in your code. See also this question: What is the difference between launch/join and async/await in Kotlin coroutines
UPDATE: Kotlin coroutines had recently introduced the concept of "Structured Concurrency" to avoid this kind of exception loss. The code in this question does not compile anymore. To compile it, you'd have to either explicitly say GlobalScope.launch
(as in "I confirm that it Ok to loose my exceptions, here is my signature") or wrap the test into runBlocking { ... }
, in which case exception is not lost.
I was able to create an exception throwing CoroutineContext
for tests.
val coroutineContext = Unconfined + CoroutineExceptionHandler { _, throwable ->
throw throwable
}
Though this would probably not be suitable for production. Maybe need to catch cancellation exceptions or something, I'm not sure