Terminating error vs non-terminating error in PowerShell
Non-terminating error
A non-terminating error reports a problem but allows PowerShell to continue running the remaining commands.
Example:
Get-Content -Path ".\MissingFile.txt"
Write-Output "The script continued."
Possible output:
Get-Content : Cannot find path '.\MissingFile.txt' because it does not exist.
The script continued.
The file-reading command failed, but PowerShell still ran the next line.
Common examples include:
Get-Content ".\MissingFile.txt"
Get-Item ".\MissingFolder"
Write-Error "A problem occurred"
By default, many PowerShell cmdlets generate non-terminating errors.
Terminating error
A terminating error stops the current operation or script block. PowerShell does not continue normally unless the error is caught.
Example using throw:
throw "The configuration file is invalid."
Write-Output "This line will not run."
The second command does not run because throw creates a terminating error.
Another example:
$result = 10 / 0
Write-Output "Finished"
Division by zero produces a terminating error.
Main difference
| Feature | Non-terminating error | Terminating error |
|---|---|---|
| Displays an error | Yes | Yes |
| Continues to the next command | Usually yes | No |
Automatically activates catch | No | Yes |
Can be converted using -ErrorAction Stop | Yes | Already terminating |
| Typical source | Cmdlet failure | throw, runtime failure, or -ErrorAction Stop |
Why try/catch sometimes does not work
Consider:
try {
Get-Content -Path ".\MissingFile.txt"
}
catch {
Write-Output "The error was caught."
}
You might expect catch to run, but Get-Content normally produces a non-terminating error.
Therefore, PowerShell displays the error but may not enter the catch block.
To make it catchable, add:
-ErrorAction Stop
Correct version:
try {
Get-Content -Path ".\MissingFile.txt" -ErrorAction Stop
}
catch {
Write-Output "The file could not be read."
}
Now the non-terminating error is converted into a terminating error, so catch runs.
Full example
try {
Write-Output "Trying to read the file..."
$content = Get-Content `
-Path ".\MissingFile.txt" `
-ErrorAction Stop
Write-Output "The file was read successfully."
}
catch {
Write-Warning "The file could not be read."
Write-Warning "Reason: $($_.Exception.Message)"
}
finally {
Write-Output "The operation is complete."
}
Expected output:
Trying to read the file...
WARNING: The file could not be read.
WARNING: Reason: Cannot find path ...
The operation is complete.
The finally block runs whether the operation succeeds or fails.
ErrorAction values
Many cmdlets support the common parameter -ErrorAction.
Get-Content ".\MissingFile.txt" -ErrorAction Continue
Common choices:
| Value | Behaviour |
|---|---|
Continue | Display the error and continue; default behaviour |
SilentlyContinue | Hide the error and continue |
Stop | Convert the error into a terminating error |
Inquire | Ask the user what to do |
Ignore | Hide the error and do not add it to $Error |
For exception handling, the most important one is:
-ErrorAction Stop
Write-Error compared with throw
Write-Error
By default, it creates a non-terminating error:
Write-Error "The server was not found."
Write-Output "The script continued."
To make it terminating:
Write-Error "The server was not found." -ErrorAction Stop
throw
throw creates a terminating error:
throw "The server was not found."
Use throw when the script cannot safely continue.
Simple rule
A non-terminating error reports a problem and usually continues. A terminating error stops normal execution and can be handled by
try/catch.
When using a cmdlet inside try, commonly write:
try {
Some-Command -ErrorAction Stop
}
catch {
Write-Warning $_.Exception.Message
}
