Bird
0
0

Why does this script not catch the error?

medium📝 Debug Q7 of 15
PowerShell - Error Handling
Why does this script not catch the error?
try { Get-Item 'nofile.txt' } catch { Write-Host 'Caught' }
ATry block is missing
BCatch block syntax is wrong
CGet-Item error is non-terminating by default
DScript has no errors
Step-by-Step Solution
Solution:
  1. Step 1: Understand default error behavior

    Get-Item produces non-terminating errors by default.
  2. Step 2: Effect on try-catch

    Non-terminating errors do not trigger catch block.
  3. Final Answer:

    Get-Item error is non-terminating by default -> Option C
  4. Quick Check:

    Non-terminating errors skip catch block [OK]
Quick Trick: Non-terminating errors don't trigger catch unless forced [OK]
Common Mistakes:
  • Assuming catch always runs
  • Ignoring error action settings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes