Bird
0
0

You run this script but it continues unexpectedly:

medium📝 Debug Q14 of 15
PowerShell - Error Handling
You run this script but it continues unexpectedly:
Try {
  Get-Item 'C:\nonexistentfile.txt' -ErrorAction Stop
} Catch {
  Write-Output 'Caught error'
}
Write-Output 'Script continues'

What is the likely cause?
AThe script stops because the Catch block is missing
BThe error is non-terminating, so Try/Catch does not catch it
CThe script stops because -ErrorAction Stop causes termination without Catch
DThe error is terminating and caught, so script continues after Catch
Step-by-Step Solution
Solution:
  1. Step 1: Analyze -ErrorAction Stop with Try/Catch

    Using -ErrorAction Stop makes the error terminating, so Try/Catch can catch it.
  2. Step 2: Understand script flow after Catch

    The Catch block runs and outputs 'Caught error', then script continues to print 'Script continues'.
  3. Final Answer:

    The error is terminating and caught, so script continues after Catch -> Option D
  4. Quick Check:

    Terminating error caught, script continues [OK]
Quick Trick: Try/Catch catches terminating errors with -ErrorAction Stop [OK]
Common Mistakes:
  • Thinking non-terminating errors trigger Catch
  • Assuming script stops without Catch block
  • Confusing error action effects on Try/Catch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes