17.1. Errors in PowerShell

In some Windows PowerShell material a distinction is drawn, conceptually and practically, between terminating errors and nonterminating errors. Information about both types of errors is stored in the $Error variable, which is described in the next section.

The dividing line between terminating and nonterminating errors is a little fuzzy and depends in part on the perceptions of the author of a PowerShell or custom cmdlet. When the cmdlet author considers that a terminating error is appropriate, then the ThrowTerminatingError() method of the System.Management.Automation.Cmdlet class is called. If the cmdlet author deems that a nonterminating error is appropriate, then the WriteError() method of the System.Management.Automation.Cmdlet class is called. For cmdlets that depend on the presence of the PowerShell runtime, the corresponding ThrowTerminatingError() and WriteError() methods of the System.Management.Automation.PSCmdlet class are used for terminating and nonterminating errors, respectively.

A terminating error, broadly, has the following characteristics:

  • It occurs when the cmdlet author considers that processing of the current object or further objects cannot be carried out in specified circumstances.

  • It is used when the cmdlet author does not want processing of the current object or further objects to be carried out in specified circumstances

  • $Error[0] contains information about the terminating error, if it is the most recent error of either kind. ...

Get Professional Windows® PowerShell 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.