如何使用Kotlin进行异常处理 - try-catch
try-catch
fun main() {
try {
// 尝试执行可能会抛出异常的代码 拼多多帮助力入口
val result = 1 / 0
} catch (e: ArithmeticException) {
// 捕获到异常时的处理
println("发生了算术异常: $e")
} finally {
// 无论是否发生异常,都会执行的代码
println("程序正常结束")
}
}
ArithmeticException1 / 0catchfinally