【发布时间】:2021-04-10 17:26:00
【问题描述】:
每当我想做一些简单的事情时,我发现每次在 Kotlin 中编写丑陋的 try/catch 语句非常烦人:
val el = collection.filter{condition}.first
el?.field // this is not going to work since 'first' can throw an exception
or
val l = someString.toLong() // This can throw NumberFormatException
有一个不错的尝试吗? Swift 中的声明:
let el = try? expression
el?.field // this will work like a charm
在 Kotlin 中有这样的东西吗?
【问题讨论】: