美文网首页Android开发Android开发经验谈
Kotlin学习笔记(十六)异常

Kotlin学习笔记(十六)异常

作者: 大虾啊啊啊 | 来源:发表于2020-10-27 09:16 被阅读0次

Kotlin的异常使用方式和Java一样,除此之外Kotlin在处理异常的时候可以使用表达式

fun main() {
    computer(11)
}
fun computer(index:Int){
    //和java一样try catch
    try {
        //使用表达式
        val status = if(index in 0..10) true else throw IllegalAccessException("参数错误")
    }
    catch (e:Exception){
        println("computer: "+e.message.toString())
    }
    println("finish")

}
computer: 参数错误
finish

相关文章

网友评论

    本文标题:Kotlin学习笔记(十六)异常

    本文链接:https://www.haomeiwen.com/subject/ohaomktx.html