0

In the Android architecture guide for the data layer, the usage of exceptions instead of a Result<T> class is recommended.

For errors that could be triggered by suspend functions, use try/catch blocks when appropriate; and in flows, use the catch operator. With this approach, the UI layer is expected to handle exceptions when calling the data layer.

In contrast to a Result<T> class I can't quite imagine how it would work with exceptions - how would I handle exceptions in the UI layer / Jetpack Compose? Are there any full examples for this? I just checked the Sunflower sample app linked in the guide but it seems not to have any network error handling at all.

3
  • I have no idea why they’re making this recommendation. The pros and cons are rather trivial and up to opinion as far as I can see. Commented Apr 7, 2023 at 16:45
  • @Tenfour04 Could you elaborate on the pro and cons? Also, I think I found one con (not sure as I am not Android expert), that is that a Flow is cancelled if an exception propagates, so it's not that useful to catch it at an high level (ViewModel level in my case). Details in stackoverflow.com/questions/75961806/… Maybe you can have a look there too :-) Commented Apr 7, 2023 at 20:48
  • Result type pro: you can’t forget to catch anything downstream in the UI layer. Con: it’s more verbose and more boilerplate. Google suggests rethrowing with more specific exception types but that still doesn’t prevent you from forgetting to catch, and that adds back some verbosity. I think they’re leaving it ambiguous what the boundary of the UI layer is. Most of their examples follow MVVM, but I’m not sure if they consider the ViewModel to be UI layer or not. Maybe what they’re getting at is that a Result class in the data layer knows too much about the UI layer. Commented Apr 7, 2023 at 22:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.