Repository navigation
Description
Note: The behavior may be an indication that there never should have been statement-terminating errors to begin with, only script-terminating ones, as reflected in the current documentation neglecting to make this important distinction.
In practice, an (uncaught) statement-terminating error (e.g., 1 / 0 or Get-Item -NoSuchParam) does not terminate the script, but can be made to do so via $ErrorActionPreference = 'Stop', against documented behavior (but not with -ErrorAction Stop).
If you do, you get:
- the usual error record reflecting the statement-terminating error in
$Error - an additional, unhelpful
$Errorentry that is simply a part of the statement-terminating-error record, namely the underlying exception.
In other words: you get 2 $Error entries, with the exception-only entry coming after (reflected in $Error[0] immediately after, with $Error[1] containing the statement-terminating-error record.
The exact relationship is:
[object]::ReferenceEquals($Error[1].exception, $Error[0]) # -> $TrueContrast this with promoting a non-terminating error to a script-terminating one, where you also get 2 entries, but the extra entry is a helpful exception ([System.Management.Automation.ActionPreferenceStopException]) whose message reflects the fact of the promotion:
# Promote a non-terminating error to a script-terminating one.
> $Error.Clear(); Get-Item -ErrorAction Stop /NoSuchItem
Get-Item : Cannot find path '/NoSuchItem' because it does not exist.
...
# Print the message of the extra $Error entry that was recorded.
# (exception System.Management.Automation.ActionPreferenceStopException)
> $Error[0].Message
The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Cannot find path '/NoSuchItem' because it does not exist.Steps to reproduce
$Error.Clear(); $ErrorActionPreference = 'stop'; Get-Item -NoSuchSwitch
'---'
$Error | % GetType | % NameExpected behavior
I'm describing expected behavior from the perspective of consistency with how things work with non-terminating errors:
Get-Item : A parameter cannot be found that matches parameter name 'NoSuchSwitch'.
...
---
ActionPreferenceStopException # indicate promotion to script-terminating error
ErrorRecord
Actual behavior
Get-Item : A parameter cannot be found that matches parameter name 'NoSuchSwitch'.
...
---
ParameterBindingException # !! Simply duplicated the .Exception value of $Error[1]
ErrorRecord
Environment data
PowerShell Core v6.0.0-beta.6 on macOS 10.12.6
PowerShell Core v6.0.0-beta.6 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.6 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.502 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Activity
KirkMunro commentedon Apr 22, 2019
Wouldn't a more appropriate expected behavior for this be a single entry in
$errorconsisting of theErrorRecordthat identifies the error, with theExceptionproperty referencing anActionPreferenceStopExceptionthat has anInnerExceptionofParameterBindingException?i.e. Why are we generating two entries in
$errorfor what an end user perceives as a single error?Similarly, when you have a
ParseException, why are we adding an exception of typeParseExceptionthat contains anErrorRecordwith anExceptionproperty of typeParentContainsErrorRecordExceptionto$error? Why don't we simply add anErrorRecordthat contains an exception of typeParseExceptioninstead?In either case, these changes could possibly be breaking for anyone who had built tooling that processes
$erroror that performs error handling, so does it matter? I'm not asking that because I feel these changes should not be made -- the fact that raw exceptions are added to$errorand they way they are added makes it more difficult to process errors since the contents of the collection are inconsistent -- but I do wonder if it's worth my time to go through and try to correct such issues (which I would do, because I'm spending a lot of time with the PowerShell parser and debugger right now). I suppose an RFC is in order to discuss the collection of issues and propose a fix that would be beneficial enough to implement.microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.