liveBook · Manning

Exception

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

An exception is an abnormal runtime condition represented by the execution environment that can interrupt or alter normal program flow.

An exception is a runtime problem that interrupts normal PowerShell execution and gives code an opportunity to respond. PowerShell commands often emit nonterminating errors and continue, so handling requires converting the relevant error into a terminating exception. section 15.1 section 15.3

Use exceptions when a failure needs an active response:

  • Set -ErrorAction Stop on the command that may fail, rather than globally suppressing errors. section 15.1 section 15.4
  • Wrap the operation in Try and handle the failure in Catch; statements after the failure in Try are skipped. section 15.4
  • Capture details with -ErrorVariable when the response depends on which exception occurred. section 15.5
  • Use typed Catch blocks for different .NET exception types and Finally for cleanup or follow-up work that must always run. section 15.7

Retrying a failed computer connection

A machine-information tool connects to one computer at a time with New-CimSession. It stops that command on failure, catches the exception, warns the user, and optionally retries with the alternate protocol. Handling one computer per iteration prevents one failure from abandoning the remaining computers. section 15.4

Try {
    # something here generates an exception
} Catch [Exception.Type.One] {
    # deal with that exception here
} Catch [Exception.Type.Two] {
    # deal with the other exception here
} Catch {
    # deal with anything else here
} Finally {
    # run something else
}

Use the Try/Catch/Finally pattern as a reusable template for typed and general exception handling. section 15.7

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Explains error conversion, Try/Catch flow, capture, typed handlers, and Finally, but stops short of detailed exception internals.
Examples
Demonstrates service lookup, computer connections, DNS hostname lookup, exception capture, typed handlers, and protocol retry.
What it assumes
Assumes PowerShell command usage and functions while teaching the distinction between errors and exceptions from the beginning.
What you can do after
Diagnose/debug with it by turning failures into catchable conditions, logging evidence, selecting responses, and preserving useful output.
How it connects
Connects exceptions with PowerShell errors, ErrorActionPreference, Try/Catch, .NET Framework methods, logging, and protocol fallback.

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 Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.