0
0
PowerShellscripting~20 mins

Try-Catch-Finally in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Try-Catch-Finally 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 try-catch-finally script?
Consider this script that tries to divide numbers and handles errors:
PowerShell
try {
  $result = 10 / 0
  Write-Output "Result: $result"
} catch {
  Write-Output "Error caught: $_"
} finally {
  Write-Output "Cleanup done"
}
A
Error caught: Attempted to divide by zero.
Cleanup done
B
Result: 0
Cleanup done
C
Error caught: $_
Cleanup done
D
Result: Infinity
Cleanup done
Attempts:
2 left
💡 Hint
Remember dividing by zero causes an exception caught by catch block.
🧠 Conceptual
intermediate
1:30remaining
Which statement about PowerShell try-catch-finally is true?
Choose the correct statement about how try-catch-finally works in PowerShell.
AThe catch block runs only if an error occurs in try.
BThe finally block runs only if no error occurs in try.
CThe catch block runs even if no error occurs in try.
DThe finally block runs only if an error occurs in try.
Attempts:
2 left
💡 Hint
Think about when catch and finally blocks execute.
🔧 Debug
advanced
2:30remaining
Why does this PowerShell script not catch the error?
Look at this script and find why the catch block never runs:
PowerShell
try {
  Write-Output "Start"
  Get-Item 'C:\nonexistentfile.txt'
  Write-Output "End"
} catch {
  Write-Output "Caught error"
} finally {
  Write-Output "Done"
}
AThe finally block prevents catch from running.
BThe catch block is missing a parameter to catch the error.
CGet-Item does not throw terminating errors by default, so catch is skipped.
DThe script syntax is invalid, so catch never runs.
Attempts:
2 left
💡 Hint
Check the type of error Get-Item throws by default.
🚀 Application
advanced
3:00remaining
What is the output of this script with nested try-catch-finally?
Analyze this nested try-catch-finally script and select the correct output:
PowerShell
try {
  Write-Output "Outer try start"
  try {
    Write-Output "Inner try start"
    throw "Inner error"
    Write-Output "Inner try end"
  } catch {
    Write-Output "Inner catch: $_"
  } finally {
    Write-Output "Inner finally"
  }
  Write-Output "Outer try end"
} catch {
  Write-Output "Outer catch: $_"
} finally {
  Write-Output "Outer finally"
}
A
Outer try start
Inner try start
Inner catch: Inner error
Inner finally
Outer finally
B
Outer try start
Inner try start
Inner finally
Outer catch: Inner error
Outer finally
C
Outer try start
Inner try start
Inner catch: Inner error
Outer catch: Inner error
Inner finally
Outer finally
D
Outer try start
Inner try start
Inner catch: Inner error
Inner finally
Outer try end
Outer finally
Attempts:
2 left
💡 Hint
Remember that the inner catch handles the inner throw, so outer catch is not triggered.
🧠 Conceptual
expert
3:00remaining
What happens if a finally block itself throws an error in PowerShell?
Consider a try-catch-finally where the finally block throws an error. What is the behavior?
AThe script terminates immediately without running catch or finally.
BThe error from finally replaces any previous error and must be handled separately.
CThe catch block catches errors from finally automatically.
DThe error in finally is ignored and does not affect script execution.
Attempts:
2 left
💡 Hint
Think about error precedence when finally throws an error.