I was trying to make an API request inside a Kotlin Coroutines. I have wrapped all code inside the coroutine with a try-catch block.
viewModelScope.launch {
try {
val temp = repository.searchMatch(query ?: "")
liveData.postValue(temp)
} catch (e: Exception) {
Log.d(TAG, e.message.toString())
}
}
The problem is, if I turning off the network while the request still running, the exception gets caught but also my app gets crashed because of the uncaught exception.
This is the uncaught exception I got:
android.system.ErrnoException: read failed: ENETDOWN (Network is down)
at libcore.io.Linux.readBytes(Native Method)
at libcore.io.Linux.read(Linux.java:184)
at libcore.io.BlockGuardOs.read(BlockGuardOs.java:254)
at android.system.Os.read(Os.java:414)
at android.net.util.PacketReader.readPacket(PacketReader.java:137)
at android.net.util.PacketReader.handleInput(PacketReader.java:207)
at android.net.util.PacketReader.access$100(PacketReader.java:70)
at android.net.util.PacketReader$1.onFileDescriptorEvents(PacketReader.java:189)
at android.os.MessageQueue.dispatchEvents(MessageQueue.java:285)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:165)
at android.os.HandlerThread.run(HandlerThread.java:65)
2020-08-13 23:48:49.326 1205-5265/? E/MtkDhcpClient: Read error
android.system.ErrnoException: read failed: ENETDOWN (Network is down)
at libcore.io.Linux.readBytes(Native Method)
at libcore.io.Linux.read(Linux.java:184)
at libcore.io.BlockGuardOs.read(BlockGuardOs.java:254)
at android.system.Os.read(Os.java:414)
at com.mediatek.net.dhcp.MtkDhcpClient$ReceiveThread.run(MtkDhcpClient.java:434)
I have tried to set a CoroutineExceptionHandler and use a SupervisorJob but nothing works. Please help me!