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