Quick Hits: Finding Exception Types with PowerShell

I had a suggestion recently after the posting of my previous article on using Try/Catch for error handling with PowerShell that mentioned that it would be useful to see how to find exceptions that could be caught explicitly vs. using a Catch {} to grab all exceptions.

So in this post I will show you a couple of ways to find an exception that you can then use with Try/Catch to be sure to handle that particular error.

The first approach is actually found in the error exception record itself. Let’s assume that we want to try and get a file or view the items using Get-ChildItem. If we are not careful, we may come across something like this:

SNAGHTMLf8f8a8f

Ok, we can see that it is an ItemNotFoundException, but just adding that as a type won’t work too well.

image

What we can do is dig into the record and pull the full name of this exception so that we can see if it is publicly available and then to use it in a Catch block. We can expand out the object by using Select –Property * to see all of the properties and hopefully make a good determination of what the exception is.

$Error[4] | Select –Property *

SNAGHTMLf948df2

We can also (and more easily) see the exception by looking at the type as well.

$Error[4].exception.GetType().fullname

image

We can see now that the full name of the exception is System.Management.Automation.ItemNotFoundException and if we verify it in the console, we can see that it doesn’t throw any errors.

image

Now let’s use that in our command again and see what happens.

Try {
    Get-ChildItem \\prox-hyperv -ErrorAction Stop
} Catch [System.Management.Automation.ItemNotFoundException] {
    Write-Verbose "Path $($_.TargetObject) not found!" -Verbose
} Catch {
    Write-Verbose "Catch all" -Verbose
}

image

As you can see, that exception is now being handled differently than if it was just another exception.

The last way to find an error exception is by iterating through all of the currently loaded exceptions in PowerShell by getting all of the exported types of each assembly. We can do this by running the following command:

[appdomain]::CurrentDomain.GetAssemblies() | ForEach {
    Try {
        $_.GetExportedTypes() | Where {
            $_.Fullname -match 'Exception'
        }
    } Catch {}
} | Select FullName

image

This is just a handful of the 325 exception types that I found on my system. There could be more or less depending on what you have loaded in your session. But with this, you can hopefully find an exception that you need for your Catch block.

There you go! A quick look at locating exceptions that can be handled with Catch individually to better isolate various errors in your code.

This entry was posted in powershell and tagged , , , , . Bookmark the permalink.

2 Responses to Quick Hits: Finding Exception Types with PowerShell

  1. Tarvinder Singh says:

    A Very nice article to explain how to get the exception of any command. Thats what I was looking for . Thanks for sharing

  2. Mike Carter says:

    Not only in finding Exception types as explained above very nicely but You know Powershell is extremely useful when it comes to Export Event log CSV. Just read this resource http://eventlogxp.com/blog/exporting-event-logs-with-windows-powershell/ which teaches how Powershell comes to rescue when exporting Error Events. So i thought i should share it here. Thanks

Leave a comment