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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access