0
0
Kotlinprogramming~10 mins

Finally block behavior in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Finally block behavior
Try block starts
Exception?
YesCatch block
Catch block ends
Try block ends
Finally block
Code after try-catch-finally
The finally block runs after try and catch blocks, no matter if an exception occurred or not.
Execution Sample
Kotlin
fun test() {
  try {
    println("Try block")
  } catch (e: Exception) {
    println("Catch block")
  } finally {
    println("Finally block")
  }
}
This code prints messages from try, catch (if exception), and finally blocks always.
Execution Table
StepActionException Thrown?Block ExecutedOutput
1Enter try blockNoTryTry block
2Try block completesNoTry
3No exception, skip catchNoCatch skipped
4Execute finally blockNoFinallyFinally block
5Exit functionNoEnd
💡 Try block finishes without exception, finally block runs, then function ends.
Variable Tracker
VariableStartAfter Step 1After Step 4Final
exceptionThrownfalsefalsefalsefalse
Key Moments - 3 Insights
Does the finally block run if there is no exception?
Yes, as shown in execution_table step 4, finally runs even when no exception occurs.
What happens if an exception is thrown in the try block?
The catch block runs first to handle it, then finally runs. This is shown in the flow diagram.
Can finally block change the return value or throw another exception?
Yes, finally runs last and can override return or throw new exceptions, but this example shows normal flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which step shows the finally block running?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Check the 'Block Executed' column for 'Finally' in execution_table row Step 4.
At which step does the program skip the catch block?
AStep 3
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the 'Block Executed' column in execution_table where it says 'Catch skipped'.
If an exception was thrown in the try block, which block would run immediately after?
AFinally block
BCatch block
CTry block again
DFunction ends immediately
💡 Hint
Refer to the concept_flow diagram showing exception path from try to catch.
Concept Snapshot
try { ... } catch(e) { ... } finally { ... }
- finally always runs after try and catch
- runs even if no exception occurs
- can override return or throw new exceptions
- used for cleanup like closing files or releasing resources
Full Transcript
In Kotlin, the finally block runs after the try and catch blocks no matter what. If the try block runs without errors, the catch block is skipped, but finally still runs. If an exception happens, the catch block runs first, then finally. This ensures cleanup code always executes. The example code prints messages from try, catch (if needed), and finally blocks. The execution table shows step-by-step which block runs and when. Variables track if an exception occurred. Key moments clarify that finally runs always, even without exceptions, and can affect return values or throw new exceptions. The quiz tests understanding of when finally runs and the order of blocks during exceptions.