Challenge - 5 Problems
PowerShell Custom Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this PowerShell script with custom error?
Consider the following script that tries to divide two numbers and shows a custom error message if division by zero occurs. What will be the output?
PowerShell
try {
$a = 10
$b = 0
$result = $a / $b
Write-Output "Result: $result"
} catch {
Write-Output "Error: Cannot divide by zero!"
}Attempts:
2 left
💡 Hint
Think about what happens when you divide by zero in PowerShell inside a try block.
✗ Incorrect
Dividing by zero in PowerShell throws a runtime exception. The catch block catches it and prints the custom error message.
💻 Command Output
intermediate2:00remaining
What does this script output when input is invalid?
This script reads a number and throws a custom error if the input is not a number. What is the output if the user inputs 'abc'?
PowerShell
try {
$input = 'abc'
if (-not ($input -as [int])) {
throw "Input is not a valid number"
}
Write-Output "Input is valid: $input"
} catch {
Write-Output "Error: $_"
}Attempts:
2 left
💡 Hint
Check how the script throws and catches the error with a custom message.
✗ Incorrect
The script throws a string error which is caught and printed as a custom error message without the full exception details.
📝 Syntax
advanced2:00remaining
Which option correctly defines a function that throws a custom error with a message?
You want to create a PowerShell function that throws a custom error with the message 'Invalid operation'. Which option is syntactically correct?
Attempts:
2 left
💡 Hint
Remember how to throw a string error message in PowerShell.
✗ Incorrect
Option B correctly throws a string error message. Option B misses quotes causing syntax error. Option B uses parentheses unnecessarily but is valid syntax. Option B tries to cast but is invalid syntax for throw.
🔧 Debug
advanced2:00remaining
Why does this script not show the custom error message?
This script is supposed to catch an error and show a custom message, but it shows the default error instead. Why?
PowerShell
try {
Write-Output "Start"
throw "Something went wrong"
} catch {
Write-Output $_.Exception.Message
}Attempts:
2 left
💡 Hint
Check what $_ contains when throwing a string error.
✗ Incorrect
When throwing a string, $_ in catch is the string itself, not an exception object, so $_.Exception is null causing no custom message.
🚀 Application
expert3:00remaining
How to create a custom error object with a specific message and error ID?
You want to throw a custom error in PowerShell with message 'File not found' and error ID 'FileMissing'. Which code snippet correctly creates and throws this error?
Attempts:
2 left
💡 Hint
ErrorRecord requires an Exception object as first argument.
✗ Incorrect
Option A correctly creates an ErrorRecord with a FileNotFoundException and throws it. Option A passes a string instead of Exception causing error. Option A throws an Exception but misses ErrorRecord wrapper. Option A passes string instead of Exception to ErrorRecord constructor causing error.