Bird
0
0

You want to create a function that throws an error if a parameter is empty. Which script correctly uses throw to do this?

hard📝 Application Q8 of 15
PowerShell - Error Handling
You want to create a function that throws an error if a parameter is empty. Which script correctly uses throw to do this?
function Test-Param {
  param([string]$input)
  if (-not $input) {
    ???
  }
  Write-Output "Input is $input"
}
Athrow Error('Parameter cannot be empty')
Bthrow 'Parameter cannot be empty'
Cthrow new Exception('Parameter cannot be empty')
Dthrow-error 'Parameter cannot be empty'
Step-by-Step Solution
Solution:
  1. Step 1: Understand throw usage in functions

    Throw expects a string or error object; simplest is a string message.
  2. Step 2: Identify valid PowerShell syntax

    throw 'Parameter cannot be empty' uses correct PowerShell syntax. throw Error('Parameter cannot be empty') and throw new Exception('Parameter cannot be empty') use syntax from other languages. throw-error 'Parameter cannot be empty' is invalid.
  3. Final Answer:

    throw 'Parameter cannot be empty' -> Option B
  4. Quick Check:

    Use throw with string message in functions [OK]
Quick Trick: Throw string message to signal errors in functions [OK]
Common Mistakes:
  • Using non-PowerShell error syntax
  • Trying to call throw like a function
  • Using invalid cmdlets

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes