Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
fun main() {
try {
println("a")
"x".toInt()
println("b")
} catch (e: NumberFormatException) {
println("c")
} finally {
println("d")
}
}a c d
- Expecting
a b c d: thinking the rest oftrystill runs after the throw. An exception jumps straight to the matchingcatch, sobis never printed. - Expecting
a c: thinkingfinallyruns only when nothing went wrong. It runs in every case, aftertryor aftercatch, sodis printed too.
- What does the program print if the text
"x"is replaced with"7"? - What happens if the
catchnamesIllegalStateExceptioninstead?