/**
* 安全的获取值的信息,其过程中发生异常会自动处理,返回null
* getValueAction 取值操作,可能发生异常
* */
inline fun <T> getValueSafely(getValueAction: () -> T?): T? {
return try {
getValueAction()
} catch(t: Throwable) {
t.printStackTrace()
null
}
}
fun testGetValueSafely() {
val value1 = getValueSafely {
1/ 0
}
val value2 = getValueSafely {
1 + 1
}
value1.printLog()
value2.printLog()
}
03-21 16:55:05.649 5072 5072 I KotlinTips: null
03-21 16:55:05.649 5072 5072 I KotlinTips: 2
KotlinTips是一个关于Kotlin编码技巧的一个系列,希望通过轻量简单的形式介绍能对大家有帮助。