try and trap catch only terminating and semi-terminating errors.
Thus, if the current error action preference is not Stop then non-terminating
errors are not caught. Note that the default PowerShell error action preference
is Continue.
In order to catch all errors use
$ErrorActionPreference = 'Stop'Scripts
- test.1.1.try.ps1 -
trydoes not catch an error. - test.1.2.try.ps1 -
trycatches an error. - test.2.1.trap.ps1 -
trapdoes not catch an error. - test.2.2.trap.ps1 -
trapcatches an error, thencontinue. - test.2.3.trap.ps1 -
trapcatches an error, thenbreak.
- Errors-of-unusual-type
- Stack Overflow Try/catch does not seem to have an effect