Spot the bug
The user leaves the screen during the load, yet the cache is still overwritten with an empty list (saveToDisk writes the file without suspending). Which line causes it?
viewModelScope.launch {val items = try {api.feed()} catch (e: Exception) {emptyList()}saveToDisk(items)}
Hint 1 of 2
What exception does cancellation throw?
Hint 2 of 2
Is it a subtype of Exception?
Choose an answer, then open Show answer below to check it.
Show answer
- Answer: line 4
} catch (e: Exception) {
catch (e: Exception) also catches the CancellationException, so the coroutine falls through, gets an empty list and runs the save. Catch IOException or rethrow cancellation.