Book Image

PowerShell Troubleshooting Guide

By : Mike Shepard
Book Image

PowerShell Troubleshooting Guide

By: Mike Shepard

Overview of this book

Table of Contents (15 chapters)
PowerShell Troubleshooting Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

PowerShell error handling


Discussions about error handling in PowerShell revolve around two things: the statements used in error handling, and what constitutes an error that needs to be handled.

The trap statement

PowerShell error handling had a rocky start. The Version 1.0 error handling statement was the trap statement. This statement is similar to the ON ERROR GOTO statement in Visual Basic 6. This was a functional way to do error handling, but it was not what most programming languages had been using for the last decade. If a trap statement is included in a scope, when an exception (called a terminating error in PowerShell terminology) occurs in that scope, the execution is stopped and the trap statement is executed. By default, trap statements handle any terminating error and can be written to only handle certain types of errors. In the scope of the trap statement, the $_ special variable contains the error or exception that was caught.

The default execution for a trap statement writes...