PowerShell catching typed exceptions

后端 未结 1 774
既然无缘
既然无缘 2020-12-15 22:07

I\'m having an issue with PowerShell where it will not catch an exception even when the exception is explicitly mentioned in the catch command.

In this case, I\'m tr

相关标签:
1条回答
  • 2020-12-15 22:16

    When you set ErrorAction to Stop, non-terminating errors are wrapped and thrown as type System.Management.Automation.ActionPreferenceStopException, this is the type you want to catch.

    try 
    {
        Get-Process -Id 123123 -ErrorAction Stop
    } 
    catch [System.Management.Automation.ActionPreferenceStopException]
    {
        ... do something ...
    }
    
    0 讨论(0)
提交回复
热议问题