Why does ErrorAction Stop matter in try/catch?
Documentation: Microsoft Learn
Learn why catch can miss a PowerShell error, how ErrorAction Stop changes cmdlet behavior, and where finally belongs.
Interview · Question 10 of 34
Answer before reveal
Your draft
Write the answer you would give in an interview. The model answer stays hidden until you have drafted at least a short response.
A strong answer
A catch block handles terminating errors from its try block. Many cmdlets report a non-terminating error and continue processing by default, so seeing red error text does not necessarily mean that execution transfers to catch. For a cmdlet whose failure should stop the current operation, use -ErrorAction Stop to promote its non-terminating errors.
Keep the try block focused on the operation you can recover from. In catch, retain useful context such as the target, operation and error record; decide whether to retry, skip that target, or rethrow. Do not turn every failure into a success message. Use finally for cleanup, such as closing a session, when cleanup is needed regardless of the outcome.
Explain that this is not a universal error adapter. A native executable can communicate failure through an exit code, and jobs or remote operations may need their own error handling. Test both the successful path and a deliberately failing path. The important result is predictable control flow and an accurate report, rather than simply suppressing visible errors.
Show it concretely
Expect this follow-up
Which errors would still require separate handling even with ErrorAction Stop?