0
0
Kotlinprogramming~20 mins

Multiple catch blocks in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Catch Blocks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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")
    }
}
ANo output
BGeneral error
CNumber format error
DCompilation error
Attempts:
2 left
💡 Hint
The exception thrown is a NumberFormatException, which is caught by the first catch block.
Predict Output
intermediate
2: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")
    }
}
AArray index out of bounds
BNull pointer exception
CGeneral exception
DNo exception caught
Attempts:
2 left
💡 Hint
Accessing an invalid array index throws ArrayIndexOutOfBoundsException.
Predict Output
advanced
2: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")
    }
}
APrints "Arithmetic error"
BRuntime error: division by zero not caught
CPrints "General error"
DCompilation error: unreachable catch block
Attempts:
2 left
💡 Hint
Division by zero throws ArithmeticException, caught by the first block. The order specific to general is valid.
Predict Output
advanced
2: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")
    }
}
AException caught
BCompilation error
CNo output
DNull pointer caught
Attempts:
2 left
💡 Hint
The !! operator throws NullPointerException if the value is null.
🧠 Conceptual
expert
3: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?
Acatch (e: Exception), catch (e: IOException), catch (e: FileNotFoundException), catch (e: Throwable)
Bcatch (e: FileNotFoundException), catch (e: IOException), catch (e: Exception), catch (e: Throwable)
Ccatch (e: Throwable), catch (e: Exception), catch (e: IOException), catch (e: FileNotFoundException)
Dcatch (e: IOException), catch (e: FileNotFoundException), catch (e: Exception), catch (e: Throwable)
Attempts:
2 left
💡 Hint
Catch blocks must go from the most specific exception to the most general.