Why does this error not catch?

Welcome Forums General PowerShell Q&A Why does this error not catch?

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
3 years, 9 months ago.

  • Author
    Posts
  • #28826

    Participant
    Topics: 11
    Replies: 8
    Points: 0
    Rank: Member

    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" }

  • #28827

    Keymaster
    Topics: 13
    Replies: 4872
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    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

    Participant
    Topics: 11
    Replies: 8
    Points: 0
    Rank: Member

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

The topic ‘Why does this error not catch?’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort