Throw and ErrorAction

The throw keyword raises a terminating error, terminating errors are not supposed to be affected by either 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 scripts 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 does not execute:

PS> Invoke-Something
Error
At line:5 char:5
+ throw 'Error'
+ ~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (Error:String) [], RuntimeException
+ FullyQualifiedErrorId : Error

If ErrorAction is set to SilentlyContinue, throw will be ignored:

PS> Invoke-Something -ErrorAction SilentlyContinue
No error

Enclosing throw in a try-catch block will trigger catch:

PS> function Invoke-Something {
[CmdletBinding()]
param ( )
try {
throw 'Error'
Write-Host 'No error'
} catch {
Write-Host 'An error occurred'
}
}
Invoke-Something -ErrorAction SilentlyContinue
An error occurred

The problem described here also applies when throw is used within the catch block. The following example should result in an error being displayed, as the error is terminating:

PS> function Invoke-Something {
[CmdletBinding()]
param ( )
try {
throw 'Error'
Write-Host 'No error'
} catch {
throw 'An error occurred'
}
}
Invoke-Something -ErrorAction SilentlyContinue

For scripts that declare the CmdletBinding attribute, ThrowTerminatingError can be used. The ThrowTerminatingError method does not suffer from the same problem:

PS> function Invoke-Something {
[CmdletBinding()]
param ( )
try {
throw 'Error'
Write-Host 'No error'
} catch {
$pscmdlet.ThrowTerminatingError($_)
}
}
Invoke-Something -ErrorAction SilentlyContinue
Invoke-Something : Error
At line:12 char:1
+ Invoke-Something -ErrorAction SilentlyContinue
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (Error:String) [Invoke-Something], RuntimeException
+ FullyQualifiedErrorId : Error,Invoke-Something

In the preceding example, throw is used to raise the original error condition (which will create an error record). ThrowTerminatingError is used to rethrow the terminating error correctly.

If a function does not use the CmdletBinding attribute, care should be taken when writing error handling. For example, the following function cannot use ThrowTerminatingError or the ErrorAction parameter, but it is still subject to ErrorActionPreference:

PS> function Invoke-Something {
throw 'Error'
Write-Host 'No error'
}
$ErrorActionPreference = 'SilentlyContinue'
Invoke-Something
No error

Workarounds for this problem include using Write-Error with ErrorAction set to Stop:

function Invoke-Something { 
    try { 
        throw 'Error' 
    } catch { 
        Write-Error -ErrorRecord $_ -ErrorAction Stop 
        break 
    } 
} 

Break or return might also be used to immediately end the function:

function Invoke-Something { 
    try { 
        throw 'Error' 
    } catch { 
        Write-Error -ErrorRecord $_ -ErrorAction Continue 
        break 
    } 
 
   Write-Host "Function end" 
} 

The error raised by Write-Error is non-terminating unless the ErrorAction parameter for Write-Error is set to Stop.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.78.102