Bird
0
0

How can you modify this script to handle both terminating and non-terminating errors?

hard📝 Application Q9 of 15
PowerShell - Error Handling
How can you modify this script to handle both terminating and non-terminating errors?
try {
  Get-Content 'file.txt'
} catch {
  Write-Output 'Error occurred'
}
ARemove try-catch and use Write-Error
BAdd a finally block after catch
CAdd -ErrorAction Stop to Get-Content command
DUse if-else instead of try-catch
Step-by-Step Solution
Solution:
  1. Step 1: Recognize non-terminating errors from Get-Content

    Get-Content produces non-terminating errors that try-catch ignores by default.
  2. Step 2: Use -ErrorAction Stop to convert errors

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

    Add -ErrorAction Stop to Get-Content command -> Option C
  4. Quick Check:

    Convert non-terminating errors with -ErrorAction Stop [OK]
Quick Trick: Use -ErrorAction Stop to catch all errors [OK]
Common Mistakes:
  • Thinking finally handles errors
  • Replacing try-catch with if-else
  • Removing error handling entirely

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes