liveBook · Manning

Chapter 24: Handling errors

What you will learn in chapter 24 of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

The chapter introduces two ways to retain PowerShell errors for later handling: the automatic $Error variable and the ErrorVariable common parameter.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces two ways to retain PowerShell errors for later handling: the automatic $Error variable and the ErrorVariable common parameter.

Key terms: $Error, $Error[0], ErrorAction, Ignore, ErrorVariable, +

24.1 Understanding errors and exceptions

Explains why PowerShell often continues after an error and how to make a command stop so code can handle the problem.

Key terms: nonterminating error, Continue, $ErrorActionPreference, -ErrorAction, Stop, -EA Stop, exception

24.2 Bad handling

This section warns that suppressing errors too broadly—or without considering what else might fail—can hide problems that a script needs to reveal.

Key terms: preference variable, -EA SilentlyContinue, Remove-Item, permission

24.3 Two reasons for exception handling

This section distinguishes handling exceptions to record problems in unattended tools from handling anticipated failures while a command runs.

Key terms: exceptions, $ErrorActionPreference, Stop, error-handling construct

24.4 Handling exceptions

Shows how to handle failures when a script connects to remote computers, including whether to stop, continue, or report errors.

Key terms: New-PSSession, terminating exception, ErrorAction, Stop, Try/Catch, foreach, ErrorVariable, computername, Catch block, warning

24.5 Handling exceptions for noncommands

Explains how to catch exceptions from operations without -ErrorAction and identifies a last-resort way to make an exception catchable.

Key terms: .NET Framework method, -ErrorAction, Try block, terminating exceptions, nonterminating exception, $ErrorActionPreference, error handling of last resort

24.6 Going further with exception handling

This section extends Try/Catch handling with exception-specific Catch blocks and Finally, and advises against using Trap in new code.

Key terms: Catch blocks, .NET Framework type name, exception, Finally block, Try, Catch, Trap, deprecated, backward compatibility, Try/Catch

24.7 Lab

Where this is used later

Concepts explained in this chapter

← Chapter 23: Adding logic and loops Chapter 25: Debugging techniques →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.