0
0
PowerShellscripting~20 mins

Why error handling prevents script failure in PowerShell - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output when error handling is used?
Consider this PowerShell script that tries to read a file. What will be the output when the file does not exist?
PowerShell
try {
  Get-Content -Path 'nonexistentfile.txt' -ErrorAction Stop
  Write-Output 'File read successfully'
} catch {
  Write-Output 'Error caught: File not found'
}
AGet-Content : Cannot find path 'nonexistentfile.txt' because it does not exist.
BError caught: File not found
CFile read successfully
DScript stops with an unhandled exception
Attempts:
2 left
💡 Hint
Think about what happens when the file is missing and how the catch block works.
🧠 Conceptual
intermediate
1:30remaining
Why use error handling in scripts?
Why is it important to use error handling like try-catch in scripts?
ATo ignore all errors silently without any message
BTo make the script run faster by skipping error checks
CTo stop the script immediately when an error occurs
DTo catch errors and allow the script to continue or handle them gracefully
Attempts:
2 left
💡 Hint
Think about what happens if errors are not handled.
🔧 Debug
advanced
2:30remaining
Identify the error handling mistake
This script is supposed to catch errors but fails to do so. What is the mistake?
PowerShell
try {
  Get-Content -Path 'missingfile.txt' -ErrorAction SilentlyContinue
  Write-Output 'File read'
} catch {
  Write-Output 'Error caught'
}
AThe -ErrorAction SilentlyContinue prevents errors from reaching catch
BThe Write-Output command is inside the try block
CGet-Content does not throw errors for missing files
DThe catch block is missing a closing brace
Attempts:
2 left
💡 Hint
Check how -ErrorAction affects error handling.
💻 Command Output
advanced
2:00remaining
What happens without error handling?
What will be the output of this script when the file does not exist?
PowerShell
Get-Content -Path 'nofile.txt'
Write-Output 'This line runs after file read'
A
Get-Content : Cannot find path 'nofile.txt' because it does not exist.
This line runs after file read
BGet-Content : Cannot find path 'nofile.txt' because it does not exist.
CScript stops with an unhandled exception before the second line
DThis line runs after file read
Attempts:
2 left
💡 Hint
Think about what happens when an error is not caught.
🚀 Application
expert
3:00remaining
How to ensure script continues after error?
You want your PowerShell script to try reading a file and if it fails, print a message but continue running other commands. Which script achieves this?
A
Get-Content 'file.txt'
Write-Output 'File missing'
Write-Output 'Script continues'
B
Get-Content 'file.txt' -ErrorAction SilentlyContinue
Write-Output 'File missing'
Write-Output 'Script continues'
C
try {
  Get-Content 'file.txt' -ErrorAction Stop
} catch {
  Write-Output 'File missing'
}
Write-Output 'Script continues'
D
try {
  Get-Content 'file.txt'
} catch {
  Write-Output 'File missing'
}
Write-Output 'Script continues'
Attempts:
2 left
💡 Hint
Consider how to make errors terminating to trigger catch and continue script.