Kotlin异常处理runCatching,getOrDefault,getOrNull run(2)
fun main(args: Array<String>) {
runCatching {
1 / 0 //发生异常
}.getOrNull().run {
println(this)
}
println("-")
runCatching {
1 / 1 //正常
}.getOrNull().run {
println(this)
}
println("--")
runCatching {
1 / 0 //发生异常
}.onFailure {
it.printStackTrace()
}.getOrDefault(2023).run {
println(this)
}
}
null
-
1
--
2023
java.lang.ArithmeticException: / by zero
at MainKt.main(Main.kt:20)
Kotlin异常处理runCatching,getOrNull,onFailure,onSuccess(1)-CSDN博客b-catch: java.lang.RuntimeException: b发生异常。kotlin异常处理try-catch-finally。kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客。https://blog.csdn.net/zhangphil/article/details/133279853
kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客b-catch: java.lang.RuntimeException: b发生异常。kotlin异常处理try-catch-finally。https://blog.csdn.net/zhangphil/article/details/129800172