Bird
Raised Fist0
PowerShellscripting~20 mins

Throw statement in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the throw statement do in PowerShell?
easy
A. It stops the script and shows an error message.
B. It continues the script without any interruption.
C. It prints a warning but does not stop the script.
D. It pauses the script and waits for user input.

Solution

  1. Step 1: Understand the purpose of throw

    The throw statement is used to stop script execution immediately and display an error message.
  2. Step 2: Compare with other options

    Options B, C, and D describe behaviors that do not match throw. Only It stops the script and shows an error message. correctly describes stopping the script with an error.
  3. Final Answer:

    It stops the script and shows an error message. -> Option A
  4. Quick Check:

    throw stops script = A [OK]
Hint: Remember: throw always stops script with error [OK]
Common Mistakes:
  • Thinking throw only shows a warning
  • Confusing throw with Write-Host or Write-Warning
  • Assuming throw pauses for input
2. Which of the following is the correct syntax to throw an error with the message "File not found" in PowerShell?
easy
A. throw -Message "File not found"
B. throw 'File not found'
C. throw File not found
D. throw(File not found)

Solution

  1. Step 1: Check correct syntax for throw message

    PowerShell requires the error message to be a string, enclosed in quotes. Both single and double quotes work, but single quotes are common.
  2. Step 2: Analyze each option

    throw -Message "File not found" is invalid (throw has no -Message parameter). throw(File not found) uses parentheses incorrectly. throw File not found misses quotes, causing syntax error. throw 'File not found' uses correct syntax.
  3. Final Answer:

    throw 'File not found' -> Option B
  4. Quick Check:

    throw 'message' syntax = D [OK]
Hint: Use quotes around error message with throw [OK]
Common Mistakes:
  • Omitting quotes around the error message
  • Using parentheses incorrectly with throw
  • Confusing throw syntax with function call
3. What will be the output of this PowerShell script?
try {
  throw 'Error occurred'
  Write-Output 'This will not run'
} catch {
  Write-Output "Caught: $_"
}
medium
A. This will not run
B. Error occurred
C. No output
D. Caught: Error occurred

Solution

  1. Step 1: Understand throw inside try block

    The throw stops execution inside try and passes control to catch with the error message.
  2. Step 2: Check catch block output

    The catch block outputs "Caught: $_" where $_ contains the error message 'Error occurred'. So output is "Caught: Error occurred".
  3. Final Answer:

    Caught: Error occurred -> Option D
  4. Quick Check:

    throw in try triggers catch output = C [OK]
Hint: throw inside try triggers catch block output [OK]
Common Mistakes:
  • Expecting code after throw to run
  • Ignoring catch block output
  • Confusing error message with direct output
4. Identify the error in this PowerShell script:
try {
  throw "Missing file"
} catch {
  Write-Output "Error: $error"
}
medium
A. The try block should not contain throw.
B. The throw statement is missing parentheses.
C. The catch block uses $error instead of $_ to get the error message.
D. Write-Output cannot be used inside catch.

Solution

  1. Step 1: Review error variable usage in catch

    Inside catch, the automatic variable $_ holds the current error. $error is an array of all errors, not the current one.
  2. Step 2: Identify correct variable for error message

    Using $error will output the entire error list, not the specific message. $_ is correct to get the thrown error message.
  3. Final Answer:

    The catch block uses $error instead of $_ to get the error message. -> Option C
  4. Quick Check:

    Use $_ in catch for error message = A [OK]
Hint: Use $_ in catch, not $error, for current error message [OK]
Common Mistakes:
  • Using $error instead of $_ in catch
  • Thinking throw needs parentheses
  • Believing throw can't be inside try
5. You want to stop a script if a variable $filePath is empty or null, showing the message "File path is required". Which code snippet correctly uses throw to do this?
hard
A. if (-not $filePath) { throw 'File path is required' }
B. if ($filePath -eq $null) throw File path is required
C. throw 'File path is required' if ($filePath -eq '')
D. if ($filePath) { throw 'File path is required' }

Solution

  1. Step 1: Check condition for empty or null variable

    Using -not $filePath correctly checks if $filePath is null, empty, or false.
  2. Step 2: Verify throw syntax and condition placement

    if (-not $filePath) { throw 'File path is required' } uses correct if statement and throw with quoted message. if ($filePath -eq $null) throw File path is required misses quotes and may cause syntax error. throw 'File path is required' if ($filePath -eq '') has wrong syntax order. if ($filePath) { throw 'File path is required' } throws when $filePath exists, which is wrong.
  3. Final Answer:

    if (-not $filePath) { throw 'File path is required' } -> Option A
  4. Quick Check:

    Use if (-not var) { throw 'msg' } = B [OK]
Hint: Use if (-not $var) { throw 'msg' } to check empty/null [OK]
Common Mistakes:
  • Forgetting quotes around error message
  • Using throw before if condition
  • Throwing error when variable is set
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard