Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rkeithhill opened this issue Sep 17, 2016 · 2 comments
Labels
Issue-Bug A bug to squash.
Milestone

Comments

@rkeithhill
Copy link
Contributor

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.

@daviwil
Copy link
Contributor

daviwil commented Sep 17, 2016

Weird, good catch!

@daviwil daviwil added the Issue-Bug A bug to squash. label Sep 17, 2016
@daviwil daviwil added this to the 0.8.0 milestone Sep 17, 2016
rkeithhill added a commit to rkeithhill/vscode-powershell that referenced this issue Sep 17, 2016
After long discussion on PSMVP alias it seems that there's a bug with catch [System.Exception].  If there are two catch statements and the "general" case uses "catch [System.Exception" instead of "catch" the general case will run instead of the more specific exception!!  That's pretty mind blowing. Fix PowerShell#296
@rkeithhill
Copy link
Contributor Author

Tracking the PowerShell issue here PowerShell/PowerShell#2293

rkeithhill added a commit that referenced this issue Sep 17, 2016
After long discussion on PSMVP alias it seems that there's a bug with catch [System.Exception].  If there are two catch statements and the "general" case uses "catch [System.Exception" instead of "catch" the general case will run instead of the more specific exception!!  That's pretty mind blowing. Fix #296
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug A bug to squash.
Projects
None yet
Development

No branches or pull requests

2 participants