Skip to content

Instantly share code, notes, and snippets.

@abdurahmanadilovic
Last active December 17, 2017 20:43
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 abdurahmanadilovic/21f9d909751d2c7359135dbe575773b4 to your computer and use it in GitHub Desktop.
Save abdurahmanadilovic/21f9d909751d2c7359135dbe575773b4 to your computer and use it in GitHub Desktop.
Exception handling with kotlin launch coroutine
fun sendCreateUserRequest(firstName: String, lastName: String) {
launch(parentJob + UI) {
try {
val listOfUsers = run(CommonPool) {
val user = User(firstName, lastName)
RestClient.apiDefinition.createUser(user).execute()
}
toast("Created user id is ${listOfUsers.body()?.id}")
} catch (ex: IOException) {
alertWithOkButton("Error with internet connection")
} catch (ex: RuntimeException) {
alertWithOkButton("Error with parsing server response")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment