-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
I’m stumbling into a detail of error handling that seems bugged. It is consistent between Windows PS 5.1 and PS 7.x, modulo how the errors are presented on the console.
• function with a try/finally
• set ErrorActionPreference = Stop at the top of the try and (intended case) reset EAP = Continue in finally
• Issue
A parameter binding error, Write-Error and Throw out of a try/finally do indeed result in a terminating error. If the finally does not reset the EAP, all three terminate the function.
If the finally resets the EAP = Continue, the parameter binding error is downgraded to a non-terminating error and the function continues. Write-Error and Throw are not downgraded and terminate the function as expected. It appears that
changing the EAP affects one class of errors in the course of the throw, while not affecting others.
In the repro, note that:
- "f" throws out of the finally (does not emit "after")
- "f -reset" stops the throw (and emits "after")
- "f -write" and "f -throw" with/without -reset all keep throwing
function f
{
param( [switch] $throw, [switch] $write, [switch] $reset )
try {
write-host EAP $ErrorActionPreference
$originalEAP = $ErrorActionPreference
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
if ($throw)
{
write-host throwing
throw 'error'
} elseif ($write) {
write-host writeerror
write-error 'error'
} else {
write-host invalidparameter
Start-Transaction -foobar foo
}
write-host continued try
}
finally
{
if ($reset) {
$ErrorActionPreference = $originalEAP
}
write-host in finally
}
write-host after
} Expected behavior
'''
PS G:\nt\src\onecore\sdktools\srvperf\vmfleet> f -reset
EAP Continue
invalidparameter
in finally
Start-Transaction:
Line |
19 | Start-Transaction -foobar foo
| ~~~~~~~~~~~~~~~~~
| The term 'Start-Transaction' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
'''Actual behavior
Note the 'after' emitted, showing that resetting the EAP affected the throwing error. Compare to behavior if using write-error/thrown error (-write and -throw respectively).
'''
PS G:\nt\src\onecore\sdktools\srvperf\vmfleet> f -reset
EAP Continue
invalidparameter
in finally
Start-Transaction:
Line |
19 | Start-Transaction -foobar foo
| ~~~~~~~~~~~~~~~~~
| The term 'Start-Transaction' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
after
'''Environment data
PS G:\nt\src\onecore\sdktools\srvperf\vmfleet> $PSVersionTable
Name Value
---- -----
PSVersion 7.0.3
PSEdition Core
GitCommitId 7.0.3
OS Microsoft Windows 10.0.19043
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0Visuals
No response