Wednesday, July 31, 2013

Finding the Error Type for Use with Trap and Try/Catch

I've read tons of web pages over last few years looking at error handling in PowerShell. Many of the blogs and examples assume that you already know the error type that you are looking for. Finally today I found an example of viewing the error type so that you can identify it and use it with Try/Catch or Trap.

To view the error type, first generate the error. This stores the error in the variable $error[0]. To view the error type use the following:
You can use this with Try/Catch to catch different error messages and perform different actions based on the specific error type.
Try {
     code to try
Catch [fullnameOfErrorType] {
     stuff to do for that error type
Catch {
     stuff to do for a non-defined error type

No comments:

Post a Comment