Skip to content

Add PowerShell-specific error-handling idioms #4

Description

Context

The baseline Error-Handling standard says fail fast, never swallow, and write helpful messages; Functions.md covers throw vs Write-Error; index.md sets $ErrorActionPreference = 'Stop'.

Gap

The PowerShell-specific error idioms are missing:

  • Call cmdlets with -ErrorAction Stop when you intend to trap them, so they raise terminating, catchable errors.
  • Around native (non-cmdlet) commands, set $ErrorActionPreference = 'Stop' and restore it afterward.
  • Put the whole transaction inside the try block rather than setting success flags to gate later code.
  • Do not rely on $? — it reports only whether the last command considered itself successful, with no detail.
  • Avoid treating a $null result as an error condition where a proper terminating error is available.
  • In a catch, copy $_ / $Error[0] into your own variable immediately, before later commands overwrite it.

Proposed change

Add a "PowerShell error handling" section to the PowerShell standard (Functions.md, or a dedicated page) that builds on the baseline Error-Handling page.

Source

PoshCode — Best Practices "Error Handling" (ERR-01…ERR-06).

Acceptance

The PowerShell standard captures the -ErrorAction Stop, transaction-in-try, avoid-$?, and copy-$_-in-catch rules.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions