Coroutine cannot catch exceptions
See original GitHub issueWhen the network requests, json has an exception, kotlin coroutine cannot catch the exception, and the application crashes.
retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
NetworkInterceptor
override fun intercept(chain: Interceptor.Chain): Response {
val request = newRequest(chain)
val response = chain.proceed(request)
return adapterData(response)
}
private fun adapterData(response: Response): Response{
response.body()?.let {body ->
var json = body.string()
//org.json.JSONException: End of input at character 0 of
val job = JSONObject(json)
...
val newBody = ResponseBody.create(body.contentType(), json)
// new response
return response.newBuilder().body(newBody).build()
}
return response
}
fun CoroutineScope.launchTryCatch(block: suspend CoroutineScope.() -> Unit,
onError: ((t: Throwable) -> Unit)?){
launch {
try {
block()
}catch (t: Throwable){
onError?.invoke(t)
}
}
}
viewModelScope.launchTryCatch({
Repository.device().groupName(group.groupId, content)
...
}, ::handleException)
Coroutine uses try/catch and still can’t catch the exception. Is there anything wrong with the usage?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Kotlin coroutine can't handle exception - Stack Overflow
In first case, when suspend function is being called in runBlocking coroutine, exception from continuation goes to catch block, and then ...
Read more >Coroutine exceptions handling | Kotlin
Coroutine exceptions handling. This section covers exception handling and cancellation on exceptions. We already know that a cancelled ...
Read more >Exceptions in coroutines. Cancellation and Exceptions in…
When a coroutine fails with an exception, it will propagate said exception up to its parent! Then, the parent will 1) cancel the...
Read more >Exception handling in Kotlin Coroutines - Kt. Academy
Catching an exception before it breaks a coroutine is helpful, but any later is too late. Communication happens via a job, so wrapping...
Read more >Are You Handling Exceptions in Kotlin Coroutines Properly?
The second possibility is to remove the exception handler, leave the try-catch logic, but change the Repository layer to use specialised ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This isn’t due to Retrofit coroutines; rather it’s in the contract for OkHttp Interceptors. They are only allowed to throw IOExceptions; any other exception is a crash and is delivered to the uncaught exception handler.
If you need an exception then use an IOException subclass, or wrap your exception in an IOException.
Ok, thanks for your reply.