Created
November 13, 2015 20:58
-
-
Save Jaykul/affc26180ae61cf629c8 to your computer and use it in GitHub Desktop.
Exceptions In PowerShell
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
#4 I call this one: where the bleep did they put the cheese? | |
function Get-FileException { | |
param($Path) | |
# Assume I'm doing something with files and it throws this exception: | |
$fnf = [System.IO.FileNotFoundException]::new("File Not Found", $Path) | |
$inf = [System.Management.Automation.ItemNotFoundException]::new( "Item Not Found", $fnf) | |
throw $inf | |
} | |
# This is the basic template for a function (since we know about Schrodinger's Exception) | |
function Test-Exception { | |
try { | |
# Imagine I'm doing a lot of work, but within that work, I call something like | |
Get-FileException "NoSuchFile.txt" | |
} catch { throw } | |
} | |
# Now you're trying to use my code, and you have nice simple error handling and logging code like ... | |
function Test-Handler { | |
$Error.Clear() | |
try { | |
Test-Exception | |
} catch { | |
Write-Host "$_ " -Fore Red -NoNewLine | |
} | |
} | |
Test-Handler | |
# Everything is wonderful. | |
# Now maybe you have some other code that throws FileNotFoundException, | |
# So you add specific handlers: | |
function Test-Handler { | |
$Error.Clear() | |
try { | |
Test-Exception | |
} catch [System.Management.Automation.ItemNotFoundException] { | |
Write-Host "ITEM NOT FOUND! " -Fore Red -NoNewLine | |
} catch [System.IO.FileNotFoundException] { | |
Write-Host "FILE NOT FOUND! " -Fore Magenta -NoNewLine | |
} | |
} | |
Test-Handler | |
# Wait, File Not Found!? Why are we catching the InnerException? | |
# Let's try reversing the catch statements: | |
function Test-Handler { | |
$Error.Clear() | |
try { | |
Test-Exception | |
} catch [System.IO.FileNotFoundException] { | |
Write-Host "FILE NOT FOUND! " -Fore Magenta -NoNewLine | |
} catch [System.Management.Automation.ItemNotFoundException] { | |
Write-Host "ITEM NOT FOUND! " -Fore Red -NoNewLine | |
} | |
} | |
Test-Handler | |
# No difference, it's still File Not Found. That's really wierd. | |
# SO you try the old code, and it's still showing ItemNotFoundException. | |
# So you start poking around: | |
function Test-Handler { | |
$Error.Clear() | |
try { | |
Test-Exception | |
} catch [System.IO.FileNotFoundException] { | |
Write-Host "FILE NOT FOUND! " -Fore Magenta -NoNewLine | |
Write-Host "$_ " -Fore Yellow -NoNewLine | |
} catch [System.Management.Automation.ItemNotFoundException] { | |
Write-Host "ITEM NOT FOUND! " -Fore Red -NoNewLine | |
Write-Host "$_ " -Fore Yellow -NoNewLine | |
} finally { | |
% -in $Error { $_.Exception.PSTypeNames -join ", " } | % { $_ } | Write-Host -Fore Cyan | |
} | |
} | |
Test-Handler | |
# OK, What the heck is going on? | |
# $Error has the ItemNotFoundException, but I can't catch it unless I stop catching FileNotFoundException? | |
# Now ... here's where it get's crazy | |
# Some other developer looks at your code and says hey ... | |
# That inner function should explicitly rethrow $_ because that's how I learned it | |
function Test-Exception { | |
try { | |
Get-FileException "NoSuchFile.txt" | |
} catch { throw $_ } | |
} | |
Test-Handler | |
# WHOAH! Our ItemNotFoundException is back! | |
# What if I used trap? | |
function Test-Exception { | |
trap { break } | |
$fnf = [System.IO.FileNotFoundException]::new("File Not Found") | |
$inf = [System.Management.Automation.ItemNotFoundException]::new( "whatever", $fnf) | |
throw $inf | |
} | |
Test-Handler | |
# Oh, heck no. Now we're back to File Not Found? | |
# What if we try the { throw $_ } trick that fixed the other one? | |
function Test-Exception { | |
trap { throw $_ } | |
$fnf = [System.IO.FileNotFoundException]::new("File Not Found") | |
$inf = [System.Management.Automation.ItemNotFoundException]::new( "whatever", $fnf) | |
throw $inf | |
} | |
Test-Handler | |
# Ok, I don't even know what just happened. $_ was an array? | |
# Maybe we have to be specific? | |
function Test-Exception { | |
trap [System.Management.Automation.ItemNotFoundException] { break } | |
$fnf = [System.IO.FileNotFoundException]::new("File Not Found") | |
$inf = [System.Management.Automation.ItemNotFoundException]::new( "whatever", $fnf) | |
throw $inf | |
} | |
Test-Handler | |
# for the love of .... | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment