Skip to content

Instantly share code, notes, and snippets.

@ryan-leap
Last active February 2, 2020 16:20
Show Gist options
  • Save ryan-leap/7ff0a82a2ce2d55a441f2b3c1d2cc5f6 to your computer and use it in GitHub Desktop.
Save ryan-leap/7ff0a82a2ce2d55a441f2b3c1d2cc5f6 to your computer and use it in GitHub Desktop.
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