Why Some Errors Aren't Caught

by Oct 28, 2015

When you receive a red error message from PowerShell, you can always encapsulate the code in a try…catch block and handle the error yourself:

  Write-Warning "Something crazy happened: $_"

However, some errors, especially originating from cmdlets, won't be handled. When that happens, you know that the missed error was handled by the error handler inside the cmdlet, and you can control the cmdlet error handler with the –ErrorAction common parameter.

When you set -ErrorAction to Stop, you actually instruct the cmdlet to throw an exception which in turn can be caught by your error handler.

To have all cmdlets emit exceptions rather than handling them internally, you may want to use $ErrorActionPreference = 'Stop', which sets the default error action for all cmdlets to 'Stop'.

Note the side effect: when you instruct a cmdlet error handler to emit an exception in case of errors, the cmdlets will immediately stop on first errors, and not continue to run.

Twitter This Tip! ReTweet this Tip!