PowerShell catching typed exceptions PowerShell catching typed exceptions powershell powershell

PowerShell catching typed exceptions


When you set ErrorAction to Stop, non-terminating errors are wrapped and thrown as typeSystem.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 ...}