Powershell: Set-StrictMode should cause errors to terminate execution.

Created on 13 May 2019  路  4Comments  路  Source: PowerShell/PowerShell

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 https://github.com/PowerShell/PowerShell-RFC/pull/88 .
I think that whatever the mechanism for causing non-zero exit codes to halt execution, that should also be included in StrictMode.

Issue-Enhancement

Most helpful comment

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: https://github.com/PowerShell/PowerShell-RFC/pull/187. That approach is designed to make opting into terminating errors actually terminating by default an explicit decision.

All 4 comments

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.

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

"allowing the rest of the probably OK data to be processed" seems contrary to the spirit of StrictMode. @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.

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: https://github.com/PowerShell/PowerShell-RFC/pull/187. That approach is designed to make opting into terminating errors actually terminating by default an explicit decision.

Was this page helpful?
0 / 5 - 0 ratings