Bird
Raised Fist0
Kotlinprogramming~20 mins

Multiple catch blocks in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using multiple catch blocks in Kotlin?
easy
A. To avoid using the try keyword
B. To run multiple try blocks at once
C. To handle different types of exceptions separately
D. To make the program run faster

Solution

  1. Step 1: Understand exception handling

    Multiple catch blocks allow handling different exceptions in different ways.
  2. Step 2: Compare options

    Only To handle different types of exceptions separately correctly describes handling different exception types separately.
  3. Final Answer:

    To handle different types of exceptions separately -> Option C
  4. Quick Check:

    Multiple catch blocks = handle exceptions separately [OK]
Hint: Remember: each catch handles one exception type [OK]
Common Mistakes:
  • Thinking multiple catch blocks run in parallel
  • Believing catch blocks replace try
  • Assuming multiple catch blocks speed up code
2. Which of the following is the correct syntax for multiple catch blocks in Kotlin?
easy
A. try { /* code */ } catch (e: IOException) { } catch (e: Exception) { }
B. try { /* code */ } catch IOException { } catch Exception { }
C. try { /* code */ } catch e: IOException { } catch e: Exception { }
D. try { /* code */ } catch (IOException e) { } catch (Exception e) { }

Solution

  1. Step 1: Recall Kotlin catch syntax

    Kotlin requires parentheses around the exception variable and type, like catch (e: Exception).
  2. Step 2: Check each option

    Only try { /* code */ } catch (e: IOException) { } catch (e: Exception) { } uses correct Kotlin syntax with parentheses and colon for type.
  3. Final Answer:

    try { /* code */ } catch (e: IOException) { } catch (e: Exception) { } -> Option A
  4. Quick Check:

    Kotlin catch syntax = catch (e: Type) [OK]
Hint: Use parentheses and colon for exception type in catch [OK]
Common Mistakes:
  • Omitting parentheses in catch
  • Using Java-style catch syntax
  • Missing colon between variable and type
3. What will be the output of this Kotlin code?
try {
  val num = "abc".toInt()
} catch (e: NumberFormatException) {
  println("Number format error")
} catch (e: Exception) {
  println("General error")
}
medium
A. Compilation error
B. General error
C. No output
D. Number format error

Solution

  1. Step 1: Identify exception thrown

    Calling "abc".toInt() throws NumberFormatException.
  2. Step 2: Match catch block

    The first catch block matches NumberFormatException, so it runs and prints "Number format error".
  3. Final Answer:

    Number format error -> Option D
  4. Quick Check:

    NumberFormatException caught first = prints message [OK]
Hint: Specific exceptions catch before general ones [OK]
Common Mistakes:
  • Assuming general catch runs first
  • Thinking no exception occurs
  • Confusing exception types
4. Find the error in this Kotlin code snippet:
try {
  val list = listOf(1, 2, 3)
  println(list[5])
} catch (e: Exception) {
  println("Error caught")
} catch (e: IndexOutOfBoundsException) {
  println("Index error")
}
medium
A. Missing finally block
B. Catch blocks are in wrong order; specific exception should come first
C. Index 5 is valid, no exception thrown
D. Try block syntax is incorrect

Solution

  1. Step 1: Understand catch block order

    Specific exceptions must come before general ones to be reachable.
  2. Step 2: Analyze given order

    The general Exception catch is before IndexOutOfBoundsException, so the latter is unreachable, causing a compile error.
  3. Final Answer:

    Catch blocks are in wrong order; specific exception should come first -> Option B
  4. Quick Check:

    Specific catch before general catch [OK]
Hint: Put specific exceptions before general ones [OK]
Common Mistakes:
  • Placing general catch before specific catch
  • Ignoring unreachable catch block error
  • Assuming finally is mandatory
5. You want to handle FileNotFoundException and IOException differently in Kotlin. Which catch block order is correct?
try {
  // file operations
} catch (e: IOException) {
  println("IO error")
} catch (e: FileNotFoundException) {
  println("File not found")
}
hard
A. Swap catch blocks: FileNotFoundException first, then IOException
B. This order is correct; IOException first, then FileNotFoundException
C. Combine both exceptions in one catch block
D. Remove catch blocks and use only finally

Solution

  1. Step 1: Understand exception hierarchy

    FileNotFoundException is a subclass of IOException.
  2. Step 2: Determine catch order

    The specific subclass FileNotFoundException must be caught before the general IOException to avoid unreachable code.
  3. Final Answer:

    Swap catch blocks: FileNotFoundException first, then IOException -> Option A
  4. Quick Check:

    Subclass catch before superclass catch [OK]
Hint: Catch subclasses before superclasses [OK]
Common Mistakes:
  • Catching superclass before subclass
  • Combining unrelated exceptions incorrectly
  • Skipping specific exception handling
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard