目录
摘要
try…catch,try 表达式
异常
- Kotlin 没有检查异常
try {
throw IOException()
} catch(e: IOException) {
println("io exception")
} catch(e: Exception) {
e.printStackTrace()
} finally {
}
Kotlin 中 try 语句也是种表达式,所以可以将它绑定到一个变量上
val a: Int = try {
val x: Int = 1 / 0
x
} catch(e: Exception) {
-1
}