It happens to all of us: our perfect script will run in to something it doesn't understand, doesn't know how to deal with, and out comes the red text. It was probably something simple, like someone mistyped the command, or that file was moved by someone trying to be "helpful". Whatever the reason, you find yourself looking at a garbled mess of angry red text that looks something like this:
Or, even worse: it was a script you had running automatically, and now you have no idea what went wrong. Fortunately, PowerShell comes with many ways to deal with errors.
Error Handling isn't a sexy or fun topic, but it is vital to writing good scripts. In this series, I hope to give you enough to get started.
What is Error Handling and what can it do?
Error Handling, simply put, is the act of performing an action when something goes wrong. Usually, these are things we expect might go wrong, or are things we've seen go wrong. It can also be used to capture random, unexpected problems, and record enough details that you can hopefully fix the problem.
The main things that Error Handling can do are 1) record information about what your script was trying to do when an error occurred, 2) attempt to fix what went wrong, either by attempting to change something, or simply trying something different, 3) allow you to control how serious an individual error is, and through that, 4) allow your script to continue working if there are other things it can still do.
What can Error Handling not do?
Error Handling can't fix things that you can't code for, or that there wasn't enough time or space to code for. Sometimes, all it can do is report that something went wrong; however, by learning how to manipulate and capture that information, you'll make identifying and fixing the problems that come up that much easier.
When should you use Error Handling?
Handle your errors early and often. With any command you put in to your script, ask yourself "What would happen to my script if this returns an error?" If there's a chance that something will return an error, you should take the time to handle it.
What ways can you handle errors?
PowerShell gives us several ways to handle errors, and with some creativity, we can come up with a few more on our own. Here are the methods I'll discuss on how to handle errors:
- Try/Catch: the standard tool in your Error Handling toolkit, Try/Catch is the main way you'll manage problems in your scripts. It's so important, I'm dedicating two parts of this series to make sure it's well covered.
- Trap: the old way to handle errors in PowerShell; I've never used it, and I've never seen it used, but I will go over it briefly simply because it used to be the only way to catch errors.
- Standard Error (or StdErr) redirect: a really sloppy, but sometimes effective, way to catch an error quickly. You'll probably only wind up using this in the shell or for really quick and dirty scripts, and it doesn't always work.
- Manual Error Handling: this is where you get creative and write your own way of handling an error. Usually, it's because Try/Catch didn't work, which can happen with poorly written cmdlets. I'll go over a couple of methods I've had good luck with when that happens.
- Ignore the error: yes, this is a way to handle the error, by explicitly not handling it, and not caring about it at all. There are times that you'll want to do this, but in general, you should avoid ignoring errors where possible.
When it comes to handling errors, the more you know about them, the easier it will be to handle each one. In the next part, I'll go over the basic concepts of controlling execution flow when an error and how to manually access exception details, as well as some simple tricks for manually handling errors. I hope to see you then!