Skip to content

Native error handling: $PSNativeCommandUseErrorActionPreference = $true unexpectedly causes non-terminating, not statement-terminating errors #18368

@mklement0

Description

@mklement0

Prerequisites

Steps to reproduce

The pre-v7.3 experimental PSNativeCommandErrorActionPreference feature allows setting the new $PSNativeCommandUseErrorActionPreference preference variable to $true, so that any call to an external (native) program that results in a nonzero exit code emits a PowerShell error (in addition to any preceding stderr output emitted by the external program itself).

For this error to be most useful, it should be a statement-terminating error - i.e. one that can be caught with try / catch, whereas it currently is a non-terminating error.

Note that the concept of a non-terminating error is a PowerShell-specific one, and doesn't really apply to external programs.

$PSNativeCommandUseErrorActionPreference = $true

try {
  # Call an external program that reports a nonzero exit code.
  & ($IsWindows ? 'cmd' : 'sh') ($IsWindows ? '/c' : '-c') 'exit 1'
}
catch {
  'caught!'
}
'after'

Expected behavior

caught!
after

Actual behavior

NativeCommandExitException: Program "sh" ended with non-zero exit code: 1.
after

That is, the unexpectedly non-terminating error wasn't caught.

Error details

No response

Environment data

PowerShell Core 7.3.0-preview.8

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.Resolution-No ActivityIssue has had no activity for 6 months or more

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions