I initialize my function with two commands. First, I load a configuration file with Get-Content
and secondly, I import a module (Import-Module
).
try {
Get-Content -Raw -Path Wrong-Path -ErrorAction Stop | ConvertFrom-Json
Import-Module -Name Wrong_Module -Force -ErrorAction Stop
}
catch [System.Management.Automation.ItemNotFoundException], [System.IO.FileNotFoundException]{
"Catch block 1"
}
catch [System.Management.Automation.RuntimeException]{
"Catch block 2"
}
catch {
"Catch block 3"
}
I tested both commands (Get-Content and Import-Module
) on their exception type but I only the second catch block is run. However, actually both commands throw an exception which should trigger the first Catch-Block.
Why is the wrong Catch-Exception block triggered?