Bird
0
0

This script is meant to handle errors but the catch block doesn't run. What is the problem?

medium📝 Debug Q14 of 15
PowerShell - Error Handling
This script is meant to handle errors but the catch block doesn't run. What is the problem?
try {
  Get-Item 'C:\nonexistentfile.txt' -ErrorAction SilentlyContinue
} catch {
  Write-Output 'Error caught.'
}
Write-Output 'Done.'
AThe -ErrorAction SilentlyContinue prevents catch from running
BThe catch block is missing
CGet-Item syntax is wrong
DWrite-Output cannot be used inside catch
Step-by-Step Solution
Solution:
  1. Step 1: Understand -ErrorAction SilentlyContinue

    This option stops errors from being thrown, so catch never triggers.
  2. Step 2: Effect on error handling

    Since no error is thrown, catch block is skipped, but script does not crash.
  3. Final Answer:

    The -ErrorAction SilentlyContinue prevents catch from running -> Option A
  4. Quick Check:

    -ErrorAction SilentlyContinue blocks catch = D [OK]
Quick Trick: SilentlyContinue stops catch from catching errors [OK]
Common Mistakes:
  • Thinking catch block is missing
  • Assuming Get-Item syntax error
  • Believing Write-Output can't be in catch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes