Challenge - 5 Problems
Multiple Catch Blocks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of multiple catch blocks with specific exceptions
What is the output of this Kotlin code with multiple catch blocks?
Kotlin
fun main() { try { val num = "abc".toInt() } catch (e: NumberFormatException) { println("Number format error") } catch (e: Exception) { println("General error") } }
Attempts:
2 left
💡 Hint
The exception thrown is a NumberFormatException, which is caught by the first catch block.
✗ Incorrect
The code tries to convert a non-numeric string to an integer, which throws NumberFormatException. The first catch block matches this exception and prints "Number format error". The second catch block is not reached.
❓ Predict Output
intermediate2:00remaining
Which catch block handles the exception?
Consider this Kotlin code snippet. Which catch block will handle the exception thrown?
Kotlin
fun main() { try { val arr = arrayOf(1, 2, 3) println(arr[5]) } catch (e: NullPointerException) { println("Null pointer exception") } catch (e: ArrayIndexOutOfBoundsException) { println("Array index out of bounds") } catch (e: Exception) { println("General exception") } }
Attempts:
2 left
💡 Hint
Accessing an invalid array index throws ArrayIndexOutOfBoundsException.
✗ Incorrect
The code tries to access index 5 in an array of size 3, which throws ArrayIndexOutOfBoundsException. The second catch block matches this exception and prints "Array index out of bounds".
❓ Predict Output
advanced2:00remaining
Multiple catch blocks with ArithmeticException
What is the output of this Kotlin code?
Kotlin
fun main() { try { val x = 10 / 0 } catch (e: ArithmeticException) { println("Arithmetic error") } catch (e: Exception) { println("General error") } catch (e: Throwable) { println("Throwable caught") } }
Attempts:
2 left
💡 Hint
Division by zero throws ArithmeticException, caught by the first block. The order specific to general is valid.
✗ Incorrect
The division 10 / 0 throws an ArithmeticException, caught by the first catch block, printing "Arithmetic error". The catch blocks are ordered correctly from most specific (ArithmeticException subclass of Exception subclass of Throwable), so no compilation error. Subsequent blocks are not reached.
❓ Predict Output
advanced2:00remaining
Output when multiple exceptions could match
What will this Kotlin program print?
Kotlin
fun main() { try { val s: String? = null println(s!!.length) } catch (e: NullPointerException) { println("Null pointer caught") } catch (e: Exception) { println("Exception caught") } }
Attempts:
2 left
💡 Hint
The !! operator throws NullPointerException if the value is null.
✗ Incorrect
The code tries to access length of a null string using !!, which throws NullPointerException. The first catch block matches and prints "Null pointer caught".
🧠 Conceptual
expert3:00remaining
Order of catch blocks and exception hierarchy
Given the following Kotlin exception hierarchy: Throwable > Exception > IOException > FileNotFoundException, which order of catch blocks is correct to catch exceptions from most specific to most general?
Attempts:
2 left
💡 Hint
Catch blocks must go from the most specific exception to the most general.
✗ Incorrect
The correct order is from the most specific subclass to the most general superclass: FileNotFoundException (most specific), IOException, Exception, Throwable (most general). This ensures each catch block is reachable.