0
0
Kotlinprogramming~15 mins

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

Choose your learning style9 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use println(result) to show the output.