Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get the error type of an exception (so you can write code to catch that particular exception)
# Quick bit of code to get the error type of an exception so that
# you can write code to catch that particular exception
Try { 1 / 0 } Catch { "Exception Error Type is: [$($_.Exception.GetType().FullName)]"}
# Running the above:
# Exception Error Type is: [System.Management.Automation.RuntimeException]
# Now use the output to catch that specific error type
Try { 1 / 0 } Catch [System.Management.Automation.RuntimeException] { "Caught it!" }
# Running the above:
# Caught it!
# Credit to Boe Prox for explaning:
# https://learn-powershell.net/2015/04/09/quick-hits-finding-exception-types-with-powershell/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment