Skip to content

try snippets use "catch [System.Exception]" and there's a potential bug with this approach #296

Closed
@rkeithhill

Description

@rkeithhill

If you specify multiple catch statements and use [System.Exception] for the catch all, that clause will always execute.

System Details

  • Operating system name and version: Windows 10 1607
  • VS Code version: 1.5.2
  • PowerShell extension version: 0.7.2
  • Output from $PSVersionTable:
Name                           Value
----                           -----
PSVersion                      5.1.14393.187
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.187
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Issue Description

Here is the issue. Look at this code:

try {
    Get-ChildItem c:\xyzzy -ea Stop
}
catch [System.Management.Automation.ItemNotFoundException] {
    "ItemNotFoundException exception caught: $_"
}
catch [System.Exception] {
    "Base exception caught: $_"
}

Which catch statement do you expect to execute given that the exception type thrown is [System.Management.Automation.ItemNotFoundException]?

It's actually the last catch statement that executes instead of the specific catch. That's messed up.

This can be fixed by making the last catch be just "catch" with no exception type specified.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions