Why does this error not catch?

This topic contains 2 replies, has 2 voices, and was last updated by  Poseyj 2 years, 10 months ago.

  • Author
  • #28826


    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?

    new-alias start "thisshouldntwork"
    Catch { Write-Output "ERROR" }

  • #28827

    Don Jones

    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.

  • #28828


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

You must be logged in to reply to this topic.