Is there a more elegant way to write this (besides...
# announcements
e
Is there a more elegant way to write this (besides writing a helper method):
Copy code
var method: String? = null
try {
    method = jsonResponse.getString("method")
} catch (exception: JSONException) {
    Log.i(TAG, "No method provide by server.")
}
d
Copy code
val method = try {
    jsonResponse.getString("method")
} catch (exceptino: JSONException) {
    Log.i(TAG, "No method provide by server.")
    null
}
1
e
Thank you, @diesieben07!
d
"everything is an expression" is one of the best syntax improvements in Kotlin 😉
💯 2
m
Although I cringe anytime I see an exception being caught, logged, and then flow continuing with a null... Of course I can't see the whole picture, so just a knee-jerk reaction at this point.
1