Skip to content

Instantly share code, notes, and snippets.

@enginebai
Last active May 14, 2022 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enginebai/7e375f7c09b66fa5f4a08071b6540b4d to your computer and use it in GitHub Desktop.
Save enginebai/7e375f7c09b66fa5f4a08071b6540b4d to your computer and use it in GitHub Desktop.
// Try not to do this!
try {
val response = api.fetchUser(userId)
} catch (exception) {
// skip the error
}
try {
val response = api.fetchUser(userId)
} catch (e) {
// Log error
logger.error(e)
when (e) {
// Display error message to the users
is ApiException -> displayErrorMessage(e.message)
is NetworkFailureException -> displayNetworkError()
}
}
// Or provide default/fallback value
val count = try {
api.fetchProductCount()
} catch (exception) {
0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment