Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to throw an error with the message 'Invalid input'.
PowerShell
throw [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the message in quotes causes a syntax error.
Using Write-Error instead of throw does not stop script execution.
✗ Incorrect
The throw statement requires a string message enclosed in quotes to throw an error with that message.
2fill in blank
mediumComplete the code to throw a custom error object with the message 'File not found'.
PowerShell
$error = New-Object System.Exception([1])
throw $error Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the message causes an error.
Using brackets or braces instead of quotes is invalid.
✗ Incorrect
The New-Object constructor expects a string message, so the message must be in quotes.
3fill in blank
hardFix the error in the code to correctly throw an error with the message 'Access denied'.
PowerShell
throw [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving the message unquoted causes a syntax error.
Using Write-Error does not throw an exception.
✗ Incorrect
The throw statement requires a string message in quotes. Without quotes, it causes a syntax error.
4fill in blank
hardFill both blanks to throw a new exception with the message 'Operation failed'.
PowerShell
throw New-Object System.[1]([2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Error instead of Exception causes an error.
Not quoting the message causes a syntax error.
✗ Incorrect
The correct class is Exception and the message must be a quoted string.
5fill in blank
hardFill all three blanks to throw a new System.Exception with a message stored in variable $msg.
PowerShell
$msg = 'Disk full' throw New-Object System.[1]([2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting $msg in quotes passes the literal string '$msg' instead of its value.
Using Error instead of Exception causes an error.
✗ Incorrect
Use Exception class and pass the variable $msg without quotes to use its value.