Skip to content

Set-StrictMode should cause errors to terminate execution. #9591

Closed

Description

Set-StrictMode should cause errors to terminate execution.

In most languages with exceptions, an unhandled exception causes the program to terminate. However, Powershell defaults to continuing on when an unhandled exception occurs.

As a result, I think it is currently widely considered a best practice to begin all scripts with:

$ErrorActionPreference = "Stop"
Set-StrictMode -Version 3.0

Proposed change

This should be equivalent to the above:

Set-StrictMode -Version 4.0 

There is also some discussion in PowerShell/PowerShell-RFC#88 .
I think that whatever the mechanism for causing non-zero exit codes to halt execution, that should also be included in StrictMode.

Activity

  1. vexx32 commented on May 13, 2019

    @vexx32
    Collaborator

    It's not so much that PS continues on when unhandled exceptions occur... it's that many commands explicity elect to output a non-terminating error for non-serious scenarios. For example, if a particular object coming from pipeline input is mangled / unusable, a non-terminating error is usually written, allowing the rest of the probably OK data to be processed. If instead a specific non-pipelined parameter value is unusable, the error is usually considered terminating as the command cannot reliably process any input.

    Strict Mode shouldn't have any bearing on this behaviour, in my opinion, and should remain a separate setting.

  2. KalleOlaviNiemitalo commented on May 15, 2019

    @KalleOlaviNiemitalo

    Set-StrictMode -Version 3.0 already means something else, although it is undocumented. See MicrosoftDocs/PowerShell-Docs#2135.

  3. lukeschlather commented on May 23, 2019

    @lukeschlather
    Author

    "allowing the rest of the probably OK data to be processed" seems contrary to the spirit of StrictMode. Rain Sallow (/u/ta11ow) (@vexx32) you're saying that you usually use Set-StrictMode but not "ErrorActionPreference" = "Stop" ?

    It would be interesting to get a sense for what style guides in different organizations look like. I think my big concern is I would advocate any style guide do max strict mode and "ErrorActionPreference" = "Stop" and I think all my coworkers agree. So having them separate settings seems bad since it makes it easy for people who want both to be unaware of one.

  4. KirkMunro commented on Jun 13, 2019

    @KirkMunro
    Contributor

    I don't think terminating error handling should be a strict-mode feature -- it would be too easy to wreak havoc if someone turned it on and they happened to use modules or scripts that use Set-StrictMode -Version Latest.

    There is an alternate proposal on how terminating errors could terminate properly as part of a collection of 4 related RFCs, all included in this PR: PowerShell/PowerShell-RFC#187. That approach is designed to make opting into terminating errors actually terminating by default an explicit decision.

  5. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  8. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-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