0
0
C Sharp (C#)programming~10 mins

Finally block behavior in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Finally block behavior
Try block starts
Code runs in try
Exception?
YesCatch block runs
Catch code runs
Try block ends
Catch ends
Finally block runs ALWAYS
Code after try-catch-finally
The finally block runs no matter what happens in try or catch, even if an exception occurs or not.
Execution Sample
C Sharp (C#)
try {
  Console.WriteLine("Try block");
  throw new Exception();
} catch {
  Console.WriteLine("Catch block");
} finally {
  Console.WriteLine("Finally block");
}
This code shows try throwing an exception, catch handling it, and finally always running.
Execution Table
StepCode ExecutedException Thrown?Block RunningOutput
1Console.WriteLine("Try block")NoTryTry block
2throw new Exception()YesTry
3Catch block runsHandledCatchCatch block
4Finally block runsHandledFinallyFinally block
5Code after try-catch-finallyHandledAfter
💡 Finally block runs always, then code continues after try-catch-finally.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
ExceptionThrownNoNoYesHandledHandledHandled
Key Moments - 3 Insights
Does the finally block run if an exception is thrown and caught?
Yes, as shown in execution_table rows 2, 3, and 4, finally runs after catch handles the exception.
What if no exception is thrown, does finally still run?
Yes, finally always runs after try block finishes, even if no exception occurs (not shown here but same logic).
Can finally block prevent the exception from propagating?
No, finally runs after catch handles exceptions; it cannot stop exceptions by itself (see execution_table row 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at Step 3?
ATry block
BCatch block
CFinally block
DNo output
💡 Hint
Check the Output column at Step 3 in execution_table.
At which step does the finally block run?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for 'Finally block runs' in the Code Executed column.
If the exception was not thrown, which step would be skipped?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Catch block runs only if exception is thrown (Step 3).
Concept Snapshot
try { ... } catch { ... } finally { ... }
- finally block runs always, after try and catch
- runs even if exception thrown or not
- useful for cleanup actions
- exception handled in catch before finally
- code after finally runs next
Full Transcript
This example shows how the finally block behaves in C#. The try block runs first. If an exception is thrown, the catch block runs to handle it. Regardless of exception or not, the finally block always runs after try and catch. This ensures cleanup code runs no matter what. The execution table shows each step, the output printed, and when the exception is thrown and handled. The variable tracker shows the exception state changing from no to yes and then handled. Key moments clarify that finally runs always, even if no exception, and it cannot stop exceptions by itself. The quiz questions help check understanding of when finally runs and what outputs appear.