Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Finally block behavior in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Finally Block Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of try-finally without catch
What is the output of this C# code snippet?
C Sharp (C#)
using System;
class Program {
  static void Main() {
    try {
      Console.WriteLine("Start");
    } finally {
      Console.WriteLine("Finally block executed");
    }
  }
}
AFinally block executed\nStart
BNo output
CStart\nFinally block executed
DStart
Attempts:
2 left
💡 Hint
Remember that the finally block always runs after the try block, even if no exception occurs.
Predict Output
intermediate
2:00remaining
Finally block with exception thrown
What will be printed when this code runs?
C Sharp (C#)
using System;
class Program {
  static void Main() {
    try {
      Console.WriteLine("Before exception");
      throw new Exception("Error");
    } finally {
      Console.WriteLine("In finally");
    }
  }
}
ABefore exception\nIn finally\nUnhandled Exception: Error
BIn finally\nBefore exception\nUnhandled Exception: Error
CBefore exception\nUnhandled Exception: Error
DNo output
Attempts:
2 left
💡 Hint
The finally block runs even if an exception is thrown, before the exception propagates.
Predict Output
advanced
2:00remaining
Return value with finally modifying variable
What is the output of this program?
C Sharp (C#)
using System;
class Program {
  static int Test() {
    int x = 1;
    try {
      return x;
    } finally {
      x = 2;
      Console.WriteLine("Finally executed");
    }
  }
  static void Main() {
    Console.WriteLine(Test());
  }
}
AFinally executed\n1
BFinally executed\n2
C1\nFinally executed
D2\nFinally executed
Attempts:
2 left
💡 Hint
The return value is determined before finally runs, even if finally changes the variable.
Predict Output
advanced
2:00remaining
Finally block with return inside it
What will this program print?
C Sharp (C#)
using System;
class Program {
  static int Test() {
    try {
      return 1;
    } finally {
      return 2;
    }
  }
  static void Main() {
    Console.WriteLine(Test());
  }
}
ARuntime exception
B1
CCompilation error
D2
Attempts:
2 left
💡 Hint
A return in finally overrides any previous return from try.
🧠 Conceptual
expert
2:00remaining
Behavior of finally with exception and return
Consider this code snippet. What happens when it runs?
C Sharp (C#)
using System;
class Program {
  static int Test() {
    try {
      throw new Exception("Error");
    } finally {
      return 42;
    }
  }
  static void Main() {
    Console.WriteLine(Test());
  }
}
APrints nothing and program crashes
BPrints 42 and suppresses the exception
CThrows the exception and does not print anything
DCompilation error due to return in finally
Attempts:
2 left
💡 Hint
A return in finally block suppresses any exception thrown in try.

Practice

(1/5)
1. What is the main purpose of the finally block in C# exception handling?
easy
A. To execute code regardless of whether an exception occurs or not
B. To catch exceptions thrown in the try block
C. To declare variables used in the try block
D. To stop the program when an exception occurs

Solution

  1. Step 1: Understand the role of finally

    The finally block runs after the try and catch blocks, no matter what happens.
  2. Step 2: Identify its purpose

    It is used to run cleanup code or important steps that must always execute, regardless of exceptions.
  3. Final Answer:

    To execute code regardless of whether an exception occurs or not -> Option A
  4. Quick Check:

    finally always runs [OK]
Hint: Remember: finally always runs, no matter what [OK]
Common Mistakes:
  • Confusing finally with catch block
  • Thinking finally only runs on exceptions
  • Believing finally can catch exceptions
2. Which of the following is the correct syntax for using a finally block in C#?
easy
A. try { } catch { } finally { }
B. try { } finally { } catch { }
C. try { } catch { }
D. finally { } try { } catch { }

Solution

  1. Step 1: Recall correct order of blocks

    In C#, the order is try, then catch (optional), then finally (optional).
  2. Step 2: Match syntax

    Only try { } catch { } finally { } shows the correct order: try { } catch { } finally { }.
  3. Final Answer:

    try { } catch { } finally { } -> Option A
  4. Quick Check:

    Correct block order [OK]
Hint: Remember order: try, catch, then finally [OK]
Common Mistakes:
  • Placing finally before catch
  • Omitting try block
  • Using finally without try
3. What will be the output of the following C# code?
try {
    Console.WriteLine("Start");
    throw new Exception();
} catch {
    Console.WriteLine("Caught");
} finally {
    Console.WriteLine("Finally");
}
medium
A. Start\nFinally
B. Start\nCaught\nFinally
C. Caught\nFinally
D. Start\nCaught

Solution

  1. Step 1: Trace the try block

    "Start" is printed, then an exception is thrown.
  2. Step 2: Catch and finally execution

    The exception is caught, so "Caught" is printed, then the finally block runs printing "Finally".
  3. Final Answer:

    Start\nCaught\nFinally -> Option B
  4. Quick Check:

    try prints Start, catch prints Caught, finally prints Finally [OK]
Hint: Remember: finally runs after catch even if exception thrown [OK]
Common Mistakes:
  • Ignoring catch block output
  • Thinking finally runs before catch
  • Missing the exception thrown in try
4. Identify the error in this C# code snippet:
try {
    Console.WriteLine("Hello");
} finally {
    Console.WriteLine("Cleanup");
} catch (Exception ex) {
    Console.WriteLine("Error");
}
medium
A. The catch block must come after finally
B. No error, code is correct
C. You cannot use finally without catch
D. The finally block must come after catch

Solution

  1. Step 1: Check block order rules

    In C#, the finally block must come after all catch blocks.
  2. Step 2: Identify incorrect order

    The code places finally before catch, which is invalid syntax.
  3. Final Answer:

    The finally block must come after catch -> Option D
  4. Quick Check:

    finally after catch [OK]
Hint: Remember: catch blocks come before finally [OK]
Common Mistakes:
  • Placing finally before catch
  • Thinking finally can be before catch
  • Confusing order of blocks
5. Consider this code:
int result = 0;
try {
    result = 10 / 0;
} catch (DivideByZeroException) {
    result = 1;
} finally {
    result = 2;
}
Console.WriteLine(result);

What will be printed and why?
hard
A. 0, because division by zero stops execution before catch
B. 1, because catch sets result to 1 and finally does not change it
C. 2, because finally always runs and can overwrite result
D. Exception thrown, program crashes

Solution

  1. Step 1: Analyze exception and catch block

    Division by zero throws DivideByZeroException, caught by catch which sets result = 1.
  2. Step 2: Understand finally block effect

    The finally block runs after catch and sets result = 2, overwriting previous value.
  3. Final Answer:

    2, because finally always runs and can overwrite result -> Option C
  4. Quick Check:

    finally runs last and sets result = 2 [OK]
Hint: finally runs last and can overwrite variables set earlier [OK]
Common Mistakes:
  • Assuming catch value stays after finally
  • Thinking exception stops finally from running
  • Believing program crashes without output