liveBook · Manning

Try-catch statement

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

Try/catch is an error-handling language construct that attempts operations and handles exceptions raised during execution.

A try/catch statement attempts an operation in a Try block and responds when an exception occurs in the Catch block. Once an exception occurs, later statements in Try are skipped. In PowerShell, use -ErrorAction Stop when necessary so an error reaches Catch instead of being suppressed or continuing unnoticed. section 24.4

A practical error-handling pattern can include:

  • Try the operation, such as creating a remote session or finding a computer in Active Directory. section 22.1.2 section 24.4
  • Catch the failure and provide a warning, log the error, or create an alternate result. section 22.1.2 section 24.4
  • Use typed Catch blocks for known exception types, followed by an untyped Catch for anything else. section 24.6
  • Use an optional Finally block for work that must run after either Try or Catch. section 24.6

Classifying computer results

A loop can try Get-ADComputer for each computer. On success, it records the computer as enabled and present in Active Directory; on failure, Catch records that it is not in Active Directory. The resulting properties can then become output objects. section 22.1.2

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
}

This pattern shows typed exception handling, a general fallback, and a Finally block in one compact structure. section 24.6

Try/Catch is the structured approach recommended for new PowerShell code; Trap remains only for backward compatibility and is deprecated. ErrorVariable and $Error can preserve error information, but Try/Catch works reliably here with terminating errors. section 24.6 section 24.7

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

How deep it goes
Explains control flow, ErrorAction Stop, typed handlers, Finally, logging, and nonterminating-error limitations without descending into exception internals.
Examples
Uses remote sessions, Active Directory computer lookups, event-log parsing, service checks, and a cross-platform uptime-function lab.
What it assumes
Assumes familiarity with PowerShell commands, loops, functions, remoting, and error variables while introducing Try/Catch through administrative examples.
What you can do after
Diagnose/debug with it by catching command failures, warning users, logging errors, and producing fallback results.
How it connects
Connects exception handling with error actions, ErrorVariable, remoting, functions, logging, and Active Directory commands.

Also explained in

Compare how 10 books explain try-catch statement →

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.