Bird
Raised Fist0
Kotlinprogramming~15 mins

Finally block behavior in Kotlin - Mini Project: Build & Apply

Choose your learning style10 modes available
Finally Block Behavior
📖 Scenario: Imagine you are writing a program that processes a simple task but you want to make sure some cleanup code always runs, no matter what happens during the task.
🎯 Goal: You will create a Kotlin program that uses a try, catch, and finally block to show how the finally block always runs.
📋 What You'll Learn
Create a variable called result and set it to an empty string
Add a try block that sets result to "Try block executed"
Add a catch block that sets result to "Catch block executed"
Add a finally block that appends " and Finally block executed" to result
Print the result variable
💡 Why This Matters
🌍 Real World
In real programs, <code>finally</code> blocks are used to close files, release resources, or clean up tasks that must happen no matter what.
💼 Career
Understanding <code>try</code>, <code>catch</code>, and <code>finally</code> is important for writing safe and reliable Kotlin applications, a key skill for many software development jobs.
Progress0 / 4 steps
1
Create the result variable
Create a variable called result and set it to an empty string "".
Kotlin
Hint

Use var result = "" to create an empty string variable.

2
Add the try block
Add a try block that sets result to "Try block executed".
Kotlin
Hint

Write try { result = "Try block executed" } to set the variable inside the try block.

3
Add the catch and finally blocks
Add a catch block that sets result to "Catch block executed" and a finally block that appends " and Finally block executed" to result.
Kotlin
Hint

Use catch (e: Exception) { result = "Catch block executed" } and finally { result += " and Finally block executed" }.

4
Print the result
Write println(result) to display the final value of result.
Kotlin
Hint

Use println(result) to show the output.

Practice

(1/5)
1.

What is the main purpose of the finally block in Kotlin?

easy
A. To execute code that must run regardless of exceptions
B. To catch exceptions thrown in the try block
C. To declare variables used in try block
D. To stop program execution immediately

Solution

  1. Step 1: Understand the role of finally

    The finally block runs after try and catch, no matter what happens.
  2. Step 2: Identify its main use

    It is used to run cleanup code like closing files or releasing resources, always executing even if exceptions occur.
  3. Final Answer:

    To execute code that must run regardless of exceptions -> Option A
  4. Quick Check:

    finally always runs = A [OK]
Hint: Remember: finally always runs after try/catch [OK]
Common Mistakes:
  • Confusing finally with catch block
  • Thinking finally only runs if no exception
  • Believing finally stops program execution
2.

Which of the following is the correct syntax for a try-catch-finally block in Kotlin?

?
easy
A. catch (e: Exception) { /* handle */ } try { /* code */ } finally { /* cleanup */ }
B. try { /* code */ } finally { /* cleanup */ } catch (e: Exception) { /* handle */ }
C. try { /* code */ } catch { /* handle */ } finally { /* cleanup */ }
D. try { /* code */ } catch (e: Exception) { /* handle */ } finally { /* cleanup */ }

Solution

  1. Step 1: Recall Kotlin try-catch-finally order

    The correct order is try, then catch, then finally.
  2. Step 2: Check syntax correctness

    try { /* code */ } catch (e: Exception) { /* handle */ } finally { /* cleanup */ } follows the correct order and syntax with exception type in catch.
  3. Final Answer:

    try { /* code */ } catch (e: Exception) { /* handle */ } finally { /* cleanup */ } -> Option D
  4. Quick Check:

    try-catch-finally order = C [OK]
Hint: Remember order: try, catch, then finally [OK]
Common Mistakes:
  • Placing finally before catch
  • Omitting exception type in catch
  • Starting with catch block
3.

What will be the output of the following Kotlin code?

fun test(): Int {
    try {
        println("Try block")
        return 1
    } catch (e: Exception) {
        println("Catch block")
        return 2
    } finally {
        println("Finally block")
    }
}

fun main() {
    println("Result: ${test()}")
}
medium
A. Try block Finally block Result: 1
B. Try block Result: 1
C. Try block Catch block Finally block Result: 2
D. Finally block Result: 1

Solution

  1. Step 1: Trace the try block execution

    The try block prints "Try block" and returns 1, but before returning, finally runs.
  2. Step 2: Understand finally block behavior with return

    The finally block prints "Finally block" before the function returns the value 1.
  3. Final Answer:

    Try block Finally block Result: 1 -> Option A
  4. Quick Check:

    finally runs before return = A [OK]
Hint: finally runs even if return happens in try [OK]
Common Mistakes:
  • Ignoring finally output when return is in try
  • Assuming catch runs without exception
  • Thinking finally runs after return prints
4.

Identify the error in this Kotlin code snippet:

fun example() {
    try {
        println("Inside try")
    } finally {
        println("Inside finally")
    } catch (e: Exception) {
        println("Inside catch")
    }
}
medium
A. "catch" block must come before "finally" block
B. "finally" block must come after "catch" block
C. "catch" block cannot be used without "try"
D. No error, code is correct

Solution

  1. Step 1: Check order of try-catch-finally

    The correct order is try, then catch, then finally. Here finally comes before catch, which is invalid.
  2. Step 2: Identify correct placement

    The finally block must always come after all catch blocks.
  3. Final Answer:

    "finally" block must come after "catch" block -> Option B
  4. Quick Check:

    finally after catch = D [OK]
Hint: Catch comes before finally in try-catch-finally [OK]
Common Mistakes:
  • Placing finally before catch
  • Thinking catch can be after finally
  • Ignoring syntax order rules
5.

Consider this Kotlin function:

fun compute(): Int {
    try {
        return 10
    } finally {
        return 20
    }
}

What will compute() return and why?

hard
A. 10, because the first return in try is used
B. Compilation error due to multiple returns
C. 20, because finally's return overrides try's return
D. 0, because finally resets the return value

Solution

  1. Step 1: Analyze return in try block

    The try block returns 10, but before returning, finally executes.
  2. Step 2: Understand return in finally block

    The finally block also has a return statement, which overrides the previous return from try.
  3. Final Answer:

    20, because finally's return overrides try's return -> Option C
  4. Quick Check:

    Return in finally overrides try return = B [OK]
Hint: Return in finally overrides any previous return [OK]
Common Mistakes:
  • Assuming first return always wins
  • Thinking multiple returns cause error
  • Ignoring finally's effect on return
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