Managing Errors
PowerShell supports two classes of errors: nonterminating errors
and terminating errors. It collects both types of errors in the list
represented by the $error
automatic
variable.
Nonterminating Errors
Most errors are nonterminating errors, in that they do not halt execution of the current cmdlet, script, function, or pipeline. When a command outputs an error (via PowerShell’s error-output facilities,) PowerShell writes that error to a stream called the error output stream.
You can output a nonterminating error using the Write-Error
cmdlet (or the WriteError()
API when writing a
cmdlet).
The $ErrorActionPreference
automatic variable allows you to control how PowerShell handles
nonterminating errors. It supports the following values:
ErrorActionPreference automatic variable values | |
---|---|
Value | Meaning |
| Do not display errors. |
| Treat nonterminating errors as terminating errors. |
| Display errors, but continue execution of the current cmdlet, script, function, or pipeline. This is the default. |
| Display a prompt that asks how PowerShell should treat this error. |
Most cmdlets allow you to configure this explicitly by passing one of the above values to its ErrorAction parameter.
Terminating Errors
A terminating error halts execution of the current cmdlet, script, function, or pipeline. If a command (such as a cmdlet or .NET method call) generates a structured exception (for example, if you provide a method with parameters outside of their valid range), PowerShell ...
Get Windows PowerShell Quick Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.