Bird
0
0

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📝 Application Q15 of 15
PowerShell - Error Handling
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?
Aif (-not $filePath) { throw 'File path is required' }
Bif ($filePath -eq $null) throw File path is required
Cthrow 'File path is required' if ($filePath -eq '')
Dif ($filePath) { throw 'File path is required' }
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes