Last active
February 2, 2020 16:20
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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