Bird
0
0

You run this code:

medium📝 Debug Q14 of 15
PowerShell - Error Handling
You run this code:
Get-Item 'C:\missingfile.txt'
Write-Output $Error[0]

But the output is empty. What is the likely reason?
AThe error was cleared before accessing $Error
BGet-Item succeeded, so no error was recorded
C$Error variable is not available in this session
DYou must use $Errors instead of $Error
Step-by-Step Solution
Solution:
  1. Step 1: Check if error occurred

    Since the file is missing, Get-Item should cause an error and populate $Error.
  2. Step 2: Consider why $Error[0] is empty

    If $Error was cleared (e.g., by $Error.Clear()) before accessing, it will be empty despite the error.
  3. Final Answer:

    The error was cleared before accessing $Error -> Option A
  4. Quick Check:

    Clearing $Error empties error list [OK]
Quick Trick: Check if $Error was cleared before reading it [OK]
Common Mistakes:
  • Assuming $Error is empty if no error occurred
  • Confusing $Error with $Errors variable
  • Thinking $Error is unavailable in session

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes