Bird
0
0

You want to guarantee that a file stream is closed after reading, even if an error occurs. Which Try-Catch-Finally structure correctly ensures this?

hard📝 Application Q8 of 15
PowerShell - Error Handling
You want to guarantee that a file stream is closed after reading, even if an error occurs. Which Try-Catch-Finally structure correctly ensures this?
AOpen file in try, read in try, close file in finally
BOpen file in finally, read in try, close file in catch
COpen file in catch, read in finally, close file in try
DOpen file in try, read in catch, close file in try
Step-by-Step Solution
Solution:
  1. Step 1: Open file before reading

    File must be opened before reading, so open in try.
  2. Step 2: Read file inside try

    Reading happens in try to catch any errors.
  3. Step 3: Close file in finally

    finally block ensures file closes regardless of success or error.
  4. Final Answer:

    Open file in try, read in try, close file in finally -> Option A
  5. Quick Check:

    finally always closes resources [OK]
Quick Trick: Open/read in try, close in finally [OK]
Common Mistakes:
  • Closing file in catch only
  • Opening file in finally
  • Reading file in catch block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes