Which of the following is the correct syntax to throw an error with the message "Access denied" in PowerShell?
easy📝 Syntax Q3 of 15
PowerShell - Error Handling
Which of the following is the correct syntax to throw an error with the message "Access denied" in PowerShell?
Athrow "Access denied
Bthrow Access denied
Cthrow ("Access denied)
Dthrow 'Access denied'
Step-by-Step Solution
Solution:
Step 1: Understand string quoting in PowerShell
PowerShell supports single or double quotes for strings. Both are valid for messages.
Step 2: Check syntax correctness
throw Access denied is invalid because the message is not quoted. throw "Access denied" and throw ("Access denied") have unbalanced quotes causing syntax errors. throw 'Access denied' uses single quotes correctly.
Final Answer:
throw 'Access denied' -> Option D
Quick Check:
Throw with quoted string [OK]
Quick Trick:Always quote the error message string [OK]
Common Mistakes:
Not quoting the error message
Adding unnecessary parentheses
Using invalid syntax without quotes
Master "Error Handling" in PowerShell
9 interactive learning modes - each teaches the same concept differently