Bird
0
0

You want to ensure a file is always closed after reading, even if an exception occurs. Which code structure correctly uses try-catch-finally to achieve this?

hard🚀 Application Q8 of 15
C Sharp (C#) - Exception Handling
You want to ensure a file is always closed after reading, even if an exception occurs. Which code structure correctly uses try-catch-finally to achieve this?
Afinally { close file } try { read file } catch { handle error }
Btry { read file } finally { close file } catch { handle error }
Ccatch { handle error } try { read file } finally { close file }
Dtry { read file } catch { handle error } finally { close file }
Step-by-Step Solution
Solution:
  1. Step 1: Understand correct order of blocks

    The correct order is try, then catch, then finally.
  2. Step 2: Match code structure

    try { read file } catch { handle error } finally { close file } follows try-catch-finally order ensuring file is closed in finally block.
  3. Final Answer:

    try { read file } catch { handle error } finally { close file } -> Option D
  4. Quick Check:

    try-catch-finally order ensures cleanup [OK]
Quick Trick: Use finally to close resources after try-catch [OK]
Common Mistakes:
MISTAKES
  • Placing finally before catch
  • Omitting finally block
  • Misordering try-catch-finally blocks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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