Kotlin Exceptions
try, catch and finally handle errors. try is an expression. Kotlin has no checked exceptions. Use require and check to validate.
Example
val n = try { "abc".toInt() } catch (e: NumberFormatException) { -1 } finally { println("done") }
println(n)
fun age(a: Int) { require(a >= 0) { "age must be positive" } }
try { age(-1) } catch (e: IllegalArgumentException) { println(e.message) }Exercise
Always runs.
try {} catch (e: Exception) {} {}
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.