throw and Custom Exceptions
throw raises an exception. Derive from Exception for your own. require and check validate.
Example
class AgeError(msg: String) : Exception(msg)
fun check(a: Int) { if (a < 0) throw AgeError("negative") }
try { check(-1) } catch (e: AgeError) { println(e.message) }
runCatching { require(1 > 2) { "must hold" } }.onFailure { println(it.message) }Exercise
Validate an argument.
(x > 0)Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.