Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

catch [System.Exception] statement executes *before* more specific exception #2293

Copy link
Copy link
@rkeithhill

Description

@rkeithhill
Issue body actions

Steps to reproduce

Execute this code:

try {
    $worksAsExpected = $false
    if ($worksAsExpected) {
        throw [System.Management.Automation.ItemNotFoundException]::new()
    }
    else {
        Get-ChildItem c:\xyzzy -ea Stop
    }
}
catch [System.Management.Automation.ItemNotFoundException] {
    "ItemNotFoundException caught: $($_.Exception.GetType().FullName) - $_"
}
catch [System.Exception] {
    "DOH! Exception caught: $($_.Exception.GetType().FullName) - $_"
}

Expected behavior

This should run the catch [System.Management.Automation.ItemNotFoundException] statement and output "ItemNotFoundException caught: ...".

Actual behavior

It runs the more general catch statement and outputs:

DOH! Exception caught: System.Management.Automation.ItemNotFoundException - Cannot find path 'C:\xyzzy' because it does not exist.

Even though the exception type is System.Management.Automation.ItemNotFoundException.

If you remove [System.Exception] from the last catch statement, this script works as expected and the specific catch statement executes. OR, if you change the $worksAsExpected to $true this will directly throw that exception in which case the more specific catch executes even with [System.Exception] on the last catch.

Environment data

Windows 10 1607 - Build14393.187

> $PSVersionTable
Name                           Value
----                           -----
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
BuildVersion                   3.0.0.0
PSRemotingProtocolVersion      2.3
GitCommitId                    v6.0.0-alpha.10
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSVersion                      6.0.0-alpha
PSEdition                      Core
CLRVersion
Reactions are currently unavailable

Metadata

Metadata

Assignees

Labels

WG-Enginecore PowerShell engine, interpreter, and runtimecore PowerShell engine, interpreter, and runtime

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.