Catching Non-Terminating Errors

by Apr 17, 2014

Non-terminating errors are errors that are handled by a cmdlet internally. Most errors that can occur in cmdlets are non-terminating.

You cannot catch these errors in an error handler. So although there is an error handler in this sample, it will not catch the cmdlet error:

try
{
  Get-WmiObject -Class Win32_BIOS -ComputerName offlineIamafraid 
}
catch
{
  Write-Warning "Oops, error: $_"
} 

To catch non-terminating errors, you must turn them into terminating errors. That is done by setting the -ErrorAction to “Stop”:

try
{
  Get-WmiObject -Class Win32_BIOS -ComputerName offlineIamafraid -ErrorAction Stop
}
catch
{
  Write-Warning "Oops, error: $_"
} 

You can temporarily set $ErrorActionPreference to “Stop” if you do not want to add a -ErrorAction Stop parameter to all cmdlets within your error handler. The preference is used if a cmdlet does not explicitly specify an -ErrorAction setting.

Twitter This Tip! ReTweet this Tip!