Is it possible to return value from the CoroutineExceptionHandler to the calling method?
override suspend fun getStatus() : Model {
return withContext<Model>(Dispatchers.IO errorHandler) {
//Do something and return model
// If any other exception is thrown while multiple coroutines its passed down to errorhanlder
}
}
private val errorHandler = CoroutineExceptionHandler { _, exception ->
}
CodePudding user response:
No it's not.
This kind of handler is for general uncaught exception handling, and can be reused in many different coroutines which may expect different return values, or may not even return a value at all.
If you want to catch some exceptions and return a different value, you'll need to do it right on the spot (for instance around your withContext) with a try/catch block:
override suspend fun getStatus() : Model = try {
withContext<Model>(Dispatchers.IO) {
// Do something and return model
}
} catch(e: Exception) { // use more specific exception if you can
SomeErrorModel(...)
}
