Back to questions
JuniorTheoryCommon
What happens when you build and run this program?
fun setAge(age: Int): Int {
require(age >= 0) { "age < 0" }
return age
}
fun main() {
println(setAge(5))
println(setAge(-1))
}Prints 5, then an IllegalArgumentException: age < 0
- Expecting an IllegalStateException: mixing up
requireandcheck.requireguards arguments and throwsIllegalArgumentException;checkguards state. - Expecting nothing to be printed: an exception at run time stops the program at its own line, and
5from the first call is already on the screen.
- What changes if
requireis replaced withcheck? - How can
maincatch this exception and print its message?