1

I understand the difference between terminating and non-terminating errors. One stops the script and the other doesn't. My question is even with a terminating error like below, why doesn't it stop the script and cause write-host '2' not execute?

enter image description here

I'm under the impression $ErrorActionPreference only affects non-terminating errors behavior and not a terminating. So, no matter what the error $ErrorActionPreference is set to, a terminating error should still stop a script right? Here it is with $ErrorActionPreference set to 'Silently Continue'. I would still expect a terminating error to occur and stop execution in between. That doesn't happen though.

enter image description here

jssteele89
  • 463
  • 1
  • 4
  • 11
  • 1
    Remember one of the SO rules is no pictures of code, just text. properly formatted using the SO comment tools. [Why not upload images of code/errors?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) – postanote Nov 08 '22 at 22:32
  • Your Error Preference (aka `$ErrorActionPreference`) determines what happens when there is an error. Otherwise with `Continue` just a `throw` will halt your script. Change it to `Stop` and you'll see what I mean – Santiago Squarzon Nov 08 '22 at 22:33
  • PowerShell has _two_ types of _terminating errors_: (a) _Statement_-terminating errors, as reported by compiled cmdlets and .NET methods, which only terminate the current _statement_ by default. (b) _Script_-terminating (runspace-terminating, fatal) errors, as raised by `throw` and when `$ErrorActionPreference = 'Stop' `is in effect or when `-ErrorAction Stop` is passed to cmdlets and a _non_-terminating error occurs. See the linked duplicate for details. For an overview of PowerShell's error handling, see [this GitHub docs issue](https://github.com/PowerShell/PowerShell-Docs/issues/1583). – mklement0 Nov 09 '22 at 03:15
  • And, yes, that `$ErrorActionPreference` also applies to _terminating_ errors, whereas `-ErrorAction` does _not_, is a baffling inconsistency - see [GitHub issue #14819](https://github.com/PowerShell/PowerShell/issues/14819). – mklement0 Nov 09 '22 at 03:42

0 Answers0