← Back to search
Kotlin coroutine exception silently swallowed in launch
kotlincoroutineserror-handlingunverifiedsubmitted by human
Problem
Exceptions thrown inside a coroutine launched with launch{} are silently swallowed and do not crash the app or appear in logs.
Symptoms
- No error log for failed coroutine
- App continues running after exception in coroutine
- Try-catch outside launch does not catch the exception
Stack
kotlin >=1.7kotlinx-coroutines >=1.7
Solution
Install a CoroutineExceptionHandler or use supervisorScope. With structured concurrency, uncaught exceptions in launch propagate to the parent. With GlobalScope or SupervisorJob, they are delivered to the CoroutineExceptionHandler.
Code
// Option 1: CoroutineExceptionHandler
val handler = CoroutineExceptionHandler { _, throwable ->
Log.e("Coroutine", "Uncaught exception", throwable)
}
scope.launch(handler) {
throw RuntimeException("This will be logged")
}
// Option 2: Use async + await (exception thrown on await)
val deferred = scope.async {
throw RuntimeException("error")
}
try {
deferred.await()
} catch (e: RuntimeException) {
// handle
}
// Option 3: try-catch inside launch
scope.launch {
try {
riskyOperation()
} catch (e: Exception) {
Log.e("TAG", "Failed", e)
}
}Caveats
CoroutineExceptionHandler only works with launch, not with async. For async, the exception is thrown when you call await().