Author Posts

August 20, 2015 at 10:28 am

This first line in the try block won't get caught and gives me the associated error, but if I switch out those lines it will get caught by my catch. Why is that?

Try
{
new-alias start "thisshouldntwork"
#get-procajksdfjdksa
}
Catch { Write-Output "ERROR" }

August 20, 2015 at 10:34 am

Because most cmdlets don't generate a trappable, terminating exception by default. See "The Big Book of PowerShell Error Handling" (free, Resources menu here to find the ebooks). Essentially, you need to probably add the -ErrorAction parameter.

Some cmdlets also *never* generate a trappable, terminating exception. That's in the cmdlet design, and there's not much you can do about it.

August 20, 2015 at 10:57 am

Ah, good to know, thanks, I'll check out that book.