liveBook · Manning

Exception

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

An exception is a runtime condition representing an abnormal operation that can interrupt execution and be handled by an exception handler.

An exception is a runtime problem that can interrupt execution and be handled deliberately. PowerShell distinguishes exceptions from nonterminating errors, which commonly display an error and continue processing other items. section 24.1

Handle exceptions when a script must respond to an expected failure or preserve evidence for later diagnosis:

  • Use terminating behavior when a command failure must enter error handling; configure it with -ErrorAction Stop or, when necessary, temporarily set $ErrorActionPreference to Stop. section 24.4 section 24.5
  • Wrap risky work in Try and respond in Catch. Use ErrorVariable when processing can continue and failures should be collected rather than stopping every operation. section 24.4
  • Use multiple typed Catch blocks for different failure types and Finally for cleanup or work that must always run; prefer Try/Catch over deprecated Trap. section 24.6

Handling an unreachable remote computer

A remote session may fail because a computer is offline, nonexistent, or incompatible with the selected protocol. Run New-PSSession with -ErrorAction Stop inside Try, then Catch can warn, log the computer name, or retry with another protocol. For several computers, choose whether one failure should stop all connections or be recorded while the remaining sessions continue. section 24.4

Get-Service -Name Foo,BITS,Nobody,WinRM -ErrorAction Continue
Get-Service -Name BITS,Nobody,WinRM -ErrorAction SilentlyContinue
Get-Service -Name BITS,Nobody,WinRM -ErrorAction Inquire
Get-Service -Name BITS,Nobody,WinRM -ErrorAction Ignore
Get-Service -Name BITS,Nobody,WinRM -ErrorAction Stop

Compare Continue, SilentlyContinue, Inquire, Ignore, and Stop while running Get-Service against a missing service. section 24.1

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains terminating and nonterminating behavior, handling constructs, typed catches, and deprecated Trap, but not exception internals.
Examples
Demonstrates missing services, failed remote sessions, multi-computer loops, error logging, and .NET operations without ErrorAction.
What you can do after
Apply it via PowerShell to stop, continue, collect, log, retry, or specifically respond to command failures.
How it connects
Connects exceptions with PowerShell error streams, ErrorActionPreference, ErrorAction, ErrorVariable, Try/Catch/Finally, remote sessions, and .NET methods.

Also explained in

Compare how 25 books explain exception →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.