Powershell: trap

Created on 23 Jan 2020  路  5Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_trap?view=powershell-7

function TrapTest {
    trap {"Error found."}
    nonsenseString
}

TrapTest

Expected behavior

Error found.

Actual behavior

Error found: The term 'nonsenseString' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included verify that the path is correct, and then try
again.

Environment data

Powershell Linux 6,7
Issue-Question

Most helpful comment

I think that doc just needs to be fixed. trap doesn't silence errors unless you include continue.

function TrapTest {
    trap {
        "Error found."
        continue
    }

    nonsenseString
}

TrapTest
# Error found.

@vexx32

This looks like it might be intentional, at least in part; if a user defines a trap{} in their profile script, for example, it would make it exceedingly difficult to see when they get a command not found error at the command line, if the trap was catching that error.

Only for the profile itself. Traps propagate to child scopes, but cannot be dot sourced:

. { trap { 'something'; continue } }
throw
# Throws normally

All 5 comments

馃 One would think it should be caught, since it does appear to be a terminating error; it is caught with try/catch.

This looks like it might be intentional, at least in part; if a user defines a trap{} in their profile script, for example, it would make it exceedingly difficult to see when they get a command not found error at the command line, if the trap was catching that error.

I think that doc just needs to be fixed. trap doesn't silence errors unless you include continue.

function TrapTest {
    trap {
        "Error found."
        continue
    }

    nonsenseString
}

TrapTest
# Error found.

@vexx32

This looks like it might be intentional, at least in part; if a user defines a trap{} in their profile script, for example, it would make it exceedingly difficult to see when they get a command not found error at the command line, if the trap was catching that error.

Only for the profile itself. Traps propagate to child scopes, but cannot be dot sourced:

. { trap { 'something'; continue } }
throw
# Throws normally

Actually, it _is_ caught (note that Error is found prints), it just doesn't _suppress the error message_.

Currently, trap forces you to choose between:

  • _displaying_ the error (_after_ processing the trap script block) and _continuing_ execution (default behavior, without a flow-control statement in the trap script block)

  • _displaying_ the error and _aborting_ execution (with break in the script block)

  • _silencing_ the error, but _continuing_ execution (with continue in the script block)

If you want to _both_ silence and abort, the only thing that gets you _close_ to is to use exit or return in the script block, but that doesn't guarantee that processing aborts _overall_.

A few sample commands to illustrate the behaviors:

& { trap { "Error found." } 1/0; 'ok' }; 'after'

& { trap { "Error found."; continue } 1/0; 'ok' }; 'after'

& { trap { "Error found."; break } 1/0; 'ok' }; 'after'

& { trap { "Error found."; return } 1/0; 'ok' }; 'after'

Oops, @SeeminglyScience - wires crossed, but I hope my comment is still useful.

I've asked for the docs to be amended: https://github.com/MicrosoftDocs/PowerShell-Docs/issues/5348

@he852100, I think it's fine to close this issue now.

Was this page helpful?
0 / 5 - 0 ratings