Bird
0
0

Why is it generally discouraged to use a return statement inside a finally block in C#?

hard🧠 Conceptual Q10 of 15
C Sharp (C#) - Exception Handling
Why is it generally discouraged to use a return statement inside a finally block in C#?
ABecause return in finally causes compilation errors.
BBecause it can hide exceptions thrown in the try or catch blocks.
CBecause finally block cannot contain executable code.
DBecause finally block runs only if no exception occurs.
Step-by-Step Solution
Solution:
  1. Step 1: Understand finally block behavior

    finally runs after try and catch, even if exceptions occur.
  2. Step 2: Effect of return in finally

    Return in finally overrides any exception, hiding it from caller.
  3. Final Answer:

    Because it can hide exceptions thrown in the try or catch blocks. -> Option B
  4. Quick Check:

    return in finally hides exceptions = C [OK]
Quick Trick: Avoid return in finally to not hide exceptions [OK]
Common Mistakes:
MISTAKES
  • Thinking finally cannot have code
  • Believing return causes errors
  • Assuming finally runs only on success

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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