Error Handling – Oh No! It’s Gone Wrong!
There are, broadly, two types of problems that we encounter when using PowerShell: problems that our code encounters and problems with our code. The first type may be as simple as a FileNotFound
message in response to Get-ChildItem
. The second type can be much harder to understand, as it may involve problems related to scope, which we saw in Chapter 9, Don’t Repeat Yourself – Functions and Scriptblocks, or an unexpected divide by zero error. We’ll deal with problems that our code encounters in the first half of this chapter, and then move on to problems we may find in our code in the second half.
We’ll start by defining what an error is and look at the two types of errors our code will encounter: terminating and non-terminating errors. We will explore how PowerShell deals with both types, how we can change that behavior, and why we might want to. We will then move on to how we can catch errors so...