Skip to content

setting error action preference in finally clause affects terminating parameter binding errors #15726

@dl2n

Description

@dl2n

Prerequisites

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.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-Won't FixThe issue won't be fixed, possibly due to compatibility reason.WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions