PowerShell Terminating vs Non-Terminating Errors
A terminating error stops the command and jumps to a catch block, while a non-terminating error reports a problem and lets the script keep going. The same missing path can produce either kind, depending on ErrorAction.
A non-terminating error lets the script continue
The error display is silenced, and the script reads the message and keeps going. The catch block never runs for this error.
try { Get-Item "/definitely-not-here-xyz" -ErrorAction SilentlyContinue } catch { "Caught" }
"Message: " + $Error[0].Exception.Message
"Script continued"Stop turns the same error into a catch
With -ErrorAction Stop, the same missing path becomes a terminating error. The catch block runs and prints the message.
try { Get-Item "/definitely-not-here-xyz" -ErrorAction Stop } catch { "Caught: " + $_.Exception.Message }
"Script continued after the catch"The difference in one script
The first error is non-terminating, so the script passes it without a catch. The second error is terminating, so the catch handles it.
try { Get-Item "/definitely-not-here-xyz" -ErrorAction SilentlyContinue } catch { "Caught first" }
"Passed the first error"
try { Get-Item "/definitely-not-here-xyz" -ErrorAction Stop } catch { "Caught: " + $_.Exception.Message }
"Passed the second error"Example
$missing = "/definitely-not-here-xyz"
try { Get-Item $missing -ErrorAction SilentlyContinue } catch { "Caught first" }
"First check passed"
try { Get-Item $missing -ErrorAction Stop } catch { "Caught: " + $_.Exception.Message }
"Second check handled"Key points
- Non-terminating error: an error that reports a problem and lets the script keep running.
- Terminating error: an error that stops the current command and jumps to a catch block.
- Default behavior: Get-Item on a missing path is non-terminating, so a try block around it does not enter catch.
- ErrorAction Stop: use -ErrorAction Stop to turn a non-terminating error into a terminating one.
- Catch rule: a catch block runs only for a terminating error, never for a non-terminating one.
In this example, the script stores a missing path in a variable and checks it twice. The first check is non-terminating, so the script passes it without a catch, and the second check uses -ErrorAction Stop, so the catch handles it.
Note
silencing an error with SilentlyContinue hides its display but still records it, so the script can read the message.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What happens when Get-Item on a missing path runs inside try without -ErrorAction Stop?