try{
var info: String? = null
// 检测异常
checkException(info)
// 异常处
println(info.length)
}catch (e: Exception){
println("异常:$e")
}
fun chechException(info: String?){
info ?: throw CustomException() // 如果info为空,则执行throw CustomException()
}
class CustomException: IllegalArgumentException("自定义异常")
// 输出结果
异常:com.android.test.CustomExcetion: 自定义异常
kotlin-异常处理
最新推荐文章于 2023-11-27 11:12:12 发布