Skip to content

Conversation

@ninovanhooff
Copy link
Collaborator

@ninovanhooff ninovanhooff commented Oct 17, 2025

Why is this important?

There are some errors that are Throwables but not Exceptions. These need to be handled too. I had this happen af few times in my projects.

Notes

It seems catching exception is not idiomatic and bad practice in Kotlin: https://stackoverflow.com/a/64323675/923557

Copy link
Collaborator

@sebaslogen sebaslogen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should never catch generic Exception, in theory.
The problem is that often developers create a custom errors extending from exception instead of Throwable. For example:

@ninovanhooff ninovanhooff merged commit 0d2a10e into develop Oct 20, 2025
3 checks passed
@ninovanhooff ninovanhooff deleted the catch-throwable branch October 20, 2025 08:06
Comment on lines 27 to 30
request().networkResponseToActionResult()
} catch (e: Exception) {
if (e is CancellationException) ActionResult.Error.Cancelled(e) else ActionResult.Error.Other(e)
} catch (t: Throwable) {
if (t is CancellationException) ActionResult.Error.Cancelled(t) else ActionResult.Error.Other(t)
}
Copy link
Collaborator

@sebaslogen sebaslogen Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example of where Exception catching was happening and it's not clear exactly what error was being catch.

If request() can throw an exception, like java.net.UnknownHostException we should catch that specific exception here instead of generic Exception. Plus all other recoverable Exceptions.
The request() function already returns a NetworkResponse<T, ApiErrorResponse> so the NetworkResponseAdapter library should take care of catching all these exception, which leaves me to the questions:

Is there any code inside the request() function that can throw a Throwable?
Are there other exceptions thrown inside the request() function that are not catch by the NetworkResponseAdapter library and we should recover from? E.g. parsing data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants