Skip to content

try/catch overrides $ErrorActionPreference? #5693

Closed

Description

I was very surprised that if the call to 'bar' is done from within try/catch, then its $ErrorActionPreference is ignored.

See the script below. When 'bar' is called errors are ignored as expected, but when doing it within try/catch the errors surface. Is this the expected behavior?

The script below is a simplification of my actual scenario; in my actual scenario 'foo', 'bar' and the call to 'bar' are on different modules/scripts.

function foo($i) {
    if ($i -eq 3) {
        throw 'error'
    }
}

function bar {
    $ErrorActionPreference = 'SilentlyContinue'

    for ($i = 0; $i -lt 5; $i++) {
        Write-Verbose -Verbose $i

        foo($i)
    }
}

Write-Verbose -Verbose 'No try/catch'

bar

Write-Verbose -Verbose ''

Write-Verbose -Verbose 'With try/catch' 

try {
    bar
} catch {
    Write-Verbose -Verbose $_
}  
 

VERBOSE: No try/catch
VERBOSE: 0
VERBOSE: 1
VERBOSE: 2
VERBOSE: 3
VERBOSE: 4
VERBOSE: 
VERBOSE: With try/catch
VERBOSE: 0
VERBOSE: 1
VERBOSE: 2
VERBOSE: 3
VERBOSE: error

Activity

  1. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Dec 15, 2017
  2. mklement0 commented on Dec 15, 2017

    @mklement0
    Contributor

    It's definitely surprising.

    Officially - according to the current documentation - $ErrorActionPreference should only apply to non-terminating errors, whereas it clearly does apply to a script-terminating error triggered by Throw as well (unlike the -ErrorAction parameter) - but apparently not consistently, as your example shows.

    I've added your symptom to this doc issue , where I'm trying to document PowerShell's de-facto error-handling behavior (and also how it deviates from the current documentation).

  3. narrieta commented on Dec 15, 2017

    @narrieta
    Author

    Yes, very surprising. I think this can make using 3rd-party modules (that set $ErrorActionPreference) harder to use. In my case, the caller and the function setting $ErrorActionPreference were on different modules, with a call stack about 5 levels deep. It was very surprising to add a try catch around a function in one module, and see errors popping out apparently from nowhere. I knew the section of our code that sets $ErrorActionPreference to 'SilentlyIgnore' (we have legacy code that produces known errors that can be ignored) so it was easy to understand what was happening. When using 3rd-party modules this could be a lot harder.

  4. narrieta commented on Dec 15, 2017

    @narrieta
    Author

    BTW, I understand that this behavior has been there since V2, when try/catch was introduced, so changing it now may be risky.

  5. added
    Breaking-Changebreaking change that may affect users
    WG-Enginecore PowerShell engine, interpreter, and runtime
    on Jan 15, 2021
  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 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.

  9. 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

    Breaking-Changebreaking change that may affect usersIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions