0
0
Kotlinprogramming~10 mins

Multiple catch blocks in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple catch blocks
Try block starts
Code runs
Exception occurs?
NoTry block ends normally
Yes
Check first catch block type
Matches
Run catch
Catch block ends
Continue after try-catch
The program tries code in the try block. If an exception happens, it checks each catch block in order to find a matching type. The first matching catch runs. If none match, the exception is unhandled.
Execution Sample
Kotlin
try {
    val num = "abc".toInt()
} catch (e: NumberFormatException) {
    println("Number format error")
} catch (e: Exception) {
    println("General error")
}
This code tries to convert a string to an integer. It catches NumberFormatException first, then any other Exception.
Execution Table
StepActionException Thrown?Catch Block CheckedCatch Block Matches?Output
1Enter try block, execute "abc".toInt()Yes: NumberFormatExceptionNumberFormatException catchYesNumber format error
2Catch block executedNoN/AN/ANumber format error printed
3Try-catch ends, continue programNoN/AN/ANo further output
💡 Exception caught by first matching catch block; program continues normally.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
numundefinedException thrown, no value assignedN/AN/A
eundefinedException object createdCatch block variable assignedOut of scope after catch
Key Moments - 2 Insights
Why does the first catch block run instead of the second?
Because the exception thrown is NumberFormatException, which matches the first catch block type exactly, so Kotlin runs that catch block and skips the others (see execution_table step 1).
What happens if no catch block matches the exception?
If no catch block matches, the exception is unhandled and will crash the program or propagate up (not shown in this example). The execution_table shows a matching catch, so no crash occurs here.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which catch block handles the exception?
ANo catch block handles it
BThe second catch block for Exception
CThe first catch block for NumberFormatException
DBoth catch blocks handle it
💡 Hint
Check the 'Catch Block Matches?' column in step 1 of the execution_table.
At which step does the program print "Number format error"?
AStep 1
BStep 2
CStep 3
DIt never prints
💡 Hint
Look at the 'Output' column in steps 1 and 2 of the execution_table.
If the first catch block was removed, which catch block would handle the exception?
AThe second catch block for Exception
BA new catch block would be needed
CNo catch block would handle it
DThe program would not compile
💡 Hint
Refer to the catch block order and matching logic in the concept_flow and execution_table.
Concept Snapshot
try {
  // code that might throw
} catch (e: ExceptionType1) {
  // handle ExceptionType1
} catch (e: ExceptionType2) {
  // handle ExceptionType2
}

- Kotlin checks catch blocks in order.
- First matching catch runs.
- If none match, exception is unhandled.
Full Transcript
This example shows how Kotlin handles multiple catch blocks. The try block runs code that throws a NumberFormatException. Kotlin checks the first catch block, which matches this exception type, so it runs that catch block and prints "Number format error". The second catch block is skipped. After handling, the program continues normally. If no catch block matched, the exception would be unhandled and could crash the program. This flow helps manage different error types separately.