Bird
0
0

Consider this code:

hard🚀 Application Q15 of 15
C Sharp (C#) - Exception Handling
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?
A0, because division by zero stops execution before catch
B1, because catch sets result to 1 and finally does not change it
C2, because finally always runs and can overwrite result
DException thrown, program crashes
Step-by-Step Solution
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]
Quick Trick: finally runs last and can overwrite variables set earlier [OK]
Common Mistakes:
MISTAKES
  • Assuming catch value stays after finally
  • Thinking exception stops finally from running
  • Believing program crashes without output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes