0
0
PowerShellscripting~10 mins

Throw statement in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to throw an error with the message 'Invalid input'.

PowerShell
throw [1]
Drag options to blanks, or click blank then click option'
A'Invalid input'
BInvalid input
CError 'Invalid input'
DWrite-Error 'Invalid input'
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.
2fill in blank
medium

Complete 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'
A'File not found'
B[File not found]
CFile not found
D{File not found}
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the message causes an error.
Using brackets or braces instead of quotes is invalid.
3fill in blank
hard

Fix 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'
AAccess denied
BWrite-Error 'Access denied'
C[Access denied]
D'Access denied'
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving the message unquoted causes a syntax error.
Using Write-Error does not throw an exception.
4fill in blank
hard

Fill 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'
AException
B'Operation failed'
C'Operation succeeded'
DError
Attempts:
3 left
💡 Hint
Common Mistakes
Using Error instead of Exception causes an error.
Not quoting the message causes a syntax error.
5fill in blank
hard

Fill 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'
AException
B$msg
C'$msg'
DError
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.