Challenge - 5 Problems
PowerShell Throw Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this PowerShell script with a throw statement?
Consider the following PowerShell script. What will it output when run?
PowerShell
try {
Write-Output "Start"
throw "Error occurred"
Write-Output "End"
} catch {
Write-Output "Caught: $_"
}Attempts:
2 left
💡 Hint
Remember that throw stops the script flow and jumps to catch.
✗ Incorrect
The script writes "Start", then throws an error which is caught by catch. "End" is never printed.
💻 Command Output
intermediate1:30remaining
What error message does this throw statement produce?
What error message will this PowerShell code produce?
PowerShell
throw "File not found"Attempts:
2 left
💡 Hint
Throw produces a terminating error with the message you provide.
✗ Incorrect
Throw creates a terminating error with the message "File not found" shown as a terminating error.
🔧 Debug
advanced2:00remaining
Why does this PowerShell script not catch the thrown error?
Examine the script below. Why does the catch block not run?
PowerShell
try {
throw "Oops"
} finally {
Write-Output "Cleaning up"
}
Write-Output "Done"Attempts:
2 left
💡 Hint
Catch is needed to handle errors thrown in try.
✗ Incorrect
The script has no catch block, so the thrown error is not caught. Finally runs regardless.
🚀 Application
advanced2:30remaining
Which script correctly throws a custom error with a specific error ID?
You want to throw an error with message "Invalid input" and error ID "InputError". Which script does this correctly?
Attempts:
2 left
💡 Hint
Throwing a custom error record requires wrapping an exception inside ErrorRecord.
✗ Incorrect
Option A correctly creates an ErrorRecord with an Exception, error ID, category, and target, then throws it.
🧠 Conceptual
expert3:00remaining
What happens when a throw statement is used inside a PowerShell function without try-catch?
If a PowerShell function contains a throw statement but no try-catch, what is the behavior when the function is called?
Attempts:
2 left
💡 Hint
Throw creates a terminating error unless caught.
✗ Incorrect
Without try-catch, the throw causes a terminating error that stops the script unless the caller handles it.