What is the purpose of multiple catch blocks in Kotlin?
Multiple catch blocks allow you to handle different types of exceptions separately, so you can respond appropriately to each error type.
Click to reveal answer
beginner
How do you write multiple catch blocks in Kotlin?
You write multiple catch blocks by placing several catch clauses after a try block, each catching a different exception type.
Click to reveal answer
intermediate
What happens if an exception does not match any catch block?
If no catch block matches the exception type, the exception will propagate up the call stack and may crash the program if not handled elsewhere.
Click to reveal answer
intermediate
Can you catch multiple exception types in a single catch block in Kotlin?
No, Kotlin does not support catching multiple exception types in a single catch block using the union operator (|). You need to use separate catch blocks for each exception type.
Click to reveal answer
beginner
Example: What will this Kotlin code print?
try {
val num = "abc".toInt()
} catch (e: NumberFormatException) {
println("Number format error")
} catch (e: Exception) {
println("General error")
}
It will print "Number format error" because the exception thrown is NumberFormatException, which matches the first catch block.
Click to reveal answer
What is the correct way to handle different exceptions in Kotlin?
AUse multiple catch blocks after a try block
BUse one catch block with multiple exception types separated by commas
CUse if statements inside a single catch block
DUse a finally block only
✗ Incorrect
Kotlin uses multiple catch blocks to handle different exceptions separately.
If an exception is not caught by any catch block, what happens?
AThe program continues normally
BThe exception is ignored
CThe exception propagates up and may crash the program
DThe finally block catches it automatically
✗ Incorrect
Uncaught exceptions propagate up the call stack and can crash the program if not handled.
Which catch block will handle a NullPointerException if you have these catch blocks?
1) catch (e: IOException)
2) catch (e: Exception)
ABoth
Bcatch (e: IOException)
CNeither
Dcatch (e: Exception)
✗ Incorrect
NullPointerException is a subclass of Exception, so the second catch block will handle it.
Can you reorder catch blocks arbitrarily in Kotlin?
ANo, more specific exceptions should come before general ones
BYes, order does not matter
CNo, general exceptions should come before specific ones
DYes, but only if you use finally
✗ Incorrect
More specific exceptions must come before general ones to avoid unreachable code.
What keyword starts a block where exceptions are caught in Kotlin?
Acatch
Btry
Cthrow
Dfinally
✗ Incorrect
The try keyword starts the block where exceptions might be thrown and caught.
Explain how multiple catch blocks work in Kotlin and why their order matters.
Think about catching specific exceptions before general ones.
You got /5 concepts.
Describe what happens when an exception is thrown inside a try block and how Kotlin decides which catch block to run.
Focus on how Kotlin matches exception types to catch blocks.
You got /4 concepts.
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
Step 1: Understand exception handling
Multiple catch blocks allow handling different exceptions in different ways.
Step 2: Compare options
Only To handle different types of exceptions separately correctly describes handling different exception types separately.
Final Answer:
To handle different types of exceptions separately -> Option C