Finally block behavior in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the finally block affects the running time of a program.
Does it add extra repeated work or change how long the program takes as input grows?
Analyze the time complexity of the following code snippet.
try
{
for (int i = 0; i < n; i++)
{
Console.WriteLine(i);
}
}
finally
{
Console.WriteLine("Cleanup done.");
}
This code prints numbers from 0 to n-1, then always prints a cleanup message.
- Primary operation: The
forloop printing numbers from 0 to n-1. - How many times: Exactly n times, once per number.
- Finally block operation: Runs once after the try block finishes, printing a single message.
As n grows, the loop runs more times, but the finally block runs only once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints + 1 cleanup print = 11 |
| 100 | 100 prints + 1 cleanup print = 101 |
| 1000 | 1000 prints + 1 cleanup print = 1001 |
Pattern observation: The total work grows mostly with n, the finally block adds a fixed small cost.
Time Complexity: O(n)
This means the program's running time grows linearly with the input size, and the finally block does not change that.
[X] Wrong: "The finally block runs multiple times and adds to the loop's cost."
[OK] Correct: The finally block runs only once after the try block finishes, so it adds a fixed cost, not a repeated one.
Understanding how finally blocks behave helps you reason about program flow and performance clearly, a useful skill in many coding situations.
"What if the finally block contained a loop that also runs n times? How would the time complexity change?"
Practice
finally block in C# exception handling?Solution
Step 1: Understand the role of
Thefinallyfinallyblock runs after thetryandcatchblocks, no matter what happens.Step 2: Identify its purpose
It is used to run cleanup code or important steps that must always execute, regardless of exceptions.Final Answer:
To execute code regardless of whether an exception occurs or not -> Option AQuick Check:
finally always runs [OK]
- Confusing finally with catch block
- Thinking finally only runs on exceptions
- Believing finally can catch exceptions
finally block in C#?Solution
Step 1: Recall correct order of blocks
In C#, the order istry, thencatch(optional), thenfinally(optional).Step 2: Match syntax
Only try { } catch { } finally { } shows the correct order:try { } catch { } finally { }.Final Answer:
try { } catch { } finally { } -> Option AQuick Check:
Correct block order [OK]
- Placing finally before catch
- Omitting try block
- Using finally without try
try {
Console.WriteLine("Start");
throw new Exception();
} catch {
Console.WriteLine("Caught");
} finally {
Console.WriteLine("Finally");
}Solution
Step 1: Trace the try block
"Start" is printed, then an exception is thrown.Step 2: Catch and finally execution
The exception is caught, so "Caught" is printed, then thefinallyblock runs printing "Finally".Final Answer:
Start\nCaught\nFinally -> Option BQuick Check:
try prints Start, catch prints Caught, finally prints Finally [OK]
- Ignoring catch block output
- Thinking finally runs before catch
- Missing the exception thrown in try
try {
Console.WriteLine("Hello");
} finally {
Console.WriteLine("Cleanup");
} catch (Exception ex) {
Console.WriteLine("Error");
}Solution
Step 1: Check block order rules
In C#, thefinallyblock must come after allcatchblocks.Step 2: Identify incorrect order
The code placesfinallybeforecatch, which is invalid syntax.Final Answer:
Thefinallyblock must come aftercatch-> Option DQuick Check:
finally after catch [OK]
- Placing finally before catch
- Thinking finally can be before catch
- Confusing order of blocks
int result = 0;
try {
result = 10 / 0;
} catch (DivideByZeroException) {
result = 1;
} finally {
result = 2;
}
Console.WriteLine(result);What will be printed and why?
Solution
Step 1: Analyze exception and catch block
Division by zero throwsDivideByZeroException, caught by catch which setsresult = 1.Step 2: Understand finally block effect
Thefinallyblock runs after catch and setsresult = 2, overwriting previous value.Final Answer:
2, because finally always runs and can overwrite result -> Option CQuick Check:
finally runs last and sets result = 2 [OK]
- Assuming catch value stays after finally
- Thinking exception stops finally from running
- Believing program crashes without output
