Bird
0
0

Identify the error in this PowerShell script that prevents error handling from working:

medium📝 Debug Q6 of 15
PowerShell - Error Handling
Identify the error in this PowerShell script that prevents error handling from working:
try {
  Get-Content 'missingfile.txt'
} catch {
  Write-Output 'Error caught'
}
AGet-Content cannot be used inside try block
BIncorrect catch block syntax
CWrite-Output should be outside catch block
DMissing -ErrorAction parameter to catch non-terminating errors
Step-by-Step Solution
Solution:
  1. Step 1: Understand error types in PowerShell

    Get-Content throws non-terminating errors by default, which try-catch does not catch.
  2. Step 2: Fix by adding -ErrorAction Stop

    Adding -ErrorAction Stop makes errors terminating, so catch block runs.
  3. Final Answer:

    Missing -ErrorAction parameter to catch non-terminating errors -> Option D
  4. Quick Check:

    Non-terminating errors need -ErrorAction Stop [OK]
Quick Trick: Use -ErrorAction Stop to catch non-terminating errors [OK]
Common Mistakes:
  • Assuming all errors are caught by try-catch
  • Misunderstanding catch block syntax
  • Thinking Get-Content can't be in try

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes