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
diesieben07
01/22/2020, 8:00 PM
Copy code
val method = try {
jsonResponse.getString("method")
} catch (exceptino: JSONException) {
Log.i(TAG, "No method provide by server.")
null
}
✅ 1
e
Ellen Spertus
01/22/2020, 8:03 PM
Thank you, @diesieben07!
d
diesieben07
01/22/2020, 8:03 PM
"everything is an expression" is one of the best syntax improvements in Kotlin 😉
💯 2
m
Mike
01/23/2020, 1:34 PM
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.