0
0
PowerShellscripting~20 mins

Throw statement in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Throw Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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: $_"
}
A
Start
Caught: Error occurred
B
Start
End
CCaught: Error occurred
D
Start
End
Caught: Error occurred
Attempts:
2 left
💡 Hint
Remember that throw stops the script flow and jumps to catch.
💻 Command Output
intermediate
1:30remaining
What error message does this throw statement produce?
What error message will this PowerShell code produce?
PowerShell
throw "File not found"
AException calling "throw" with "1" argument(s): "File not found"
BThrow: File not found
CTerminating error: File not found
DFile not found
Attempts:
2 left
💡 Hint
Throw produces a terminating error with the message you provide.
🔧 Debug
advanced
2: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"
ABecause throw inside try without catch causes script to stop immediately
BBecause finally blocks prevent catch from running
CBecause Write-Output after finally stops the error
DBecause there is no catch block to handle the throw
Attempts:
2 left
💡 Hint
Catch is needed to handle errors thrown in try.
🚀 Application
advanced
2: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?
Athrow [System.Management.Automation.ErrorRecord]::new((New-Object System.Exception('Invalid input')), 'InputError', [System.Management.Automation.ErrorCategory]::InvalidArgument, $null)
Bthrow [System.Management.Automation.ErrorRecord]::new('Invalid input', 'InputError', [System.Management.Automation.ErrorCategory]::InvalidArgument, $null)
Cthrow [System.Exception]::new('Invalid input', 'InputError')
Dthrow New-Object System.Exception('Invalid input') -ErrorId 'InputError'
Attempts:
2 left
💡 Hint
Throwing a custom error record requires wrapping an exception inside ErrorRecord.
🧠 Conceptual
expert
3: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?
AThe error is silently ignored and the function returns null
BThe error is thrown and stops the script unless the caller handles it
CThe function automatically catches the error and returns an error object
DThe error is converted to a warning and script continues
Attempts:
2 left
💡 Hint
Throw creates a terminating error unless caught.