February 2019
Intermediate to advanced
626 pages
15h 51m
English
The throw keyword raises a terminating error; terminating errors aren't supposed to be affected by ErrorAction or ErrorActionPreference.
Unfortunately, errors raised by throw are affected by ErrorAction when ErrorAction is set to SilentlyContinue. This behavior is an important consideration when designing commands for others to use.
The following function throws an error first; the second command should never run:
function Invoke-Something {
[CmdletBinding()]
param ( )
throw 'Error'
Write-Host 'No error'
}
Running the function normally shows that the error is thrown, and the second command doesn't execute:
PS> Invoke-SomethingErrorAt line:5 char:5+ throw 'Error'+ ~~~~~~~~~~~~~+ CategoryInfo : OperationStopped: (Error:String) ...
Read now
Unlock full access