0
0
PowerShellscripting~20 mins

Custom error messages in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Custom Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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!"
}
ADivide by zero exception thrown, script stops
BResult: Infinity
CResult: 0
DError: Cannot divide by zero!
Attempts:
2 left
💡 Hint
Think about what happens when you divide by zero in PowerShell inside a try block.
💻 Command Output
intermediate
2: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: $_"
}
AError: Input is not a valid number
BError: System.Management.Automation.RuntimeException: Input is not a valid number
CInput is valid: abc
DNo output
Attempts:
2 left
💡 Hint
Check how the script throws and catches the error with a custom message.
📝 Syntax
advanced
2: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?
Afunction Test-Error { throw Invalid operation }
Bfunction Test-Error { throw "Invalid operation" }
Cfunction Test-Error { throw ('Invalid operation') }
Dfunction Test-Error { throw [string]'Invalid operation' }
Attempts:
2 left
💡 Hint
Remember how to throw a string error message in PowerShell.
🔧 Debug
advanced
2: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
}
ABecause Write-Output cannot print error messages
BBecause throw does not create an exception object
CBecause $_ is a string, not an object with Exception property
DBecause catch block is missing curly braces
Attempts:
2 left
💡 Hint
Check what $_ contains when throwing a string error.
🚀 Application
expert
3: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?
A$err = New-Object System.Management.Automation.ErrorRecord (New-Object System.IO.FileNotFoundException 'File not found'), 'FileMissing', 'ObjectNotFound', $null; throw $err
Bthrow New-Object System.Management.Automation.ErrorRecord ('File not found', 'FileMissing', 'ObjectNotFound', $null)
Cthrow New-Object System.IO.FileNotFoundException('File not found', 'FileMissing')
D$err = New-Object System.Management.Automation.ErrorRecord ('File not found', 'FileMissing', 'ObjectNotFound', $null); throw $err
Attempts:
2 left
💡 Hint
ErrorRecord requires an Exception object as first argument.