{"action":"created","issue":{"url":"https://api.github.com/repos/PowerShell/PowerShell/issues/6200","repository_url":"https://api.github.com/repos/PowerShell/PowerShell","labels_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/6200/labels{/name}","comments_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/6200/comments","events_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/6200/events","html_url":"https://github.com/PowerShell/PowerShell/issues/6200","id":298700895,"number":6200,"title":"Handling ActionPreferenceStopException looses the actual exception","user":{"login":"Jaykul","id":192942,"avatar_url":"https://avatars2.githubusercontent.com/u/192942?v=4","gravatar_id":"","url":"https://api.github.com/users/Jaykul","html_url":"https://github.com/Jaykul","followers_url":"https://api.github.com/users/Jaykul/followers","following_url":"https://api.github.com/users/Jaykul/following{/other_user}","gists_url":"https://api.github.com/users/Jaykul/gists{/gist_id}","starred_url":"https://api.github.com/users/Jaykul/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Jaykul/subscriptions","organizations_url":"https://api.github.com/users/Jaykul/orgs","repos_url":"https://api.github.com/users/Jaykul/repos","events_url":"https://api.github.com/users/Jaykul/events{/privacy}","received_events_url":"https://api.github.com/users/Jaykul/received_events","type":"User","site_admin":false},"labels":[{"id":401720878,"url":"https://api.github.com/repos/PowerShell/PowerShell/labels/Area-Language","name":"Area-Language","color":"5319e7","default":false},{"id":423526741,"url":"https://api.github.com/repos/PowerShell/PowerShell/labels/Issue-Discussion","name":"Issue-Discussion","color":"fc2929","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-02-20T18:30:22Z","updated_at":"2018-03-17T02:18:33Z","closed_at":null,"author_association":"NONE","body":"# Exception Handling in PowerShell is the pits (part 1)\r\n### I am _re-filing_ this from Connect from as it appears to have gone missing again.\r\n\r\nSteps to reproduce\r\n------------------\r\n\r\n```powershell\r\ntry {\r\n   get-content nosuchfile -ErrorAction Stop\r\n} catch [System.Management.Automation.ActionPreferenceStopException] {\r\n   $_.PSTypeNames, $_.Exception.PSTypeNames, $_.Exception.InnerException.PSTypeNames\r\n} \r\n```\r\n\r\nOutput:\r\n```\r\nSystem.Management.Automation.ErrorRecord\r\nSystem.Object\r\nSystem.Management.Automation.ItemNotFoundException\r\nSystem.Management.Automation.SessionStateException\r\nSystem.Management.Automation.RuntimeException\r\nSystem.SystemException\r\nSystem.Exception\r\nSystem.Object\r\n```\r\n\r\n\r\nExpected behavior\r\n-----------------\r\n\r\nSince I \"caught\" an ActionPreferenceStopException, I should be able to see the ActionPreferenceStopException.\r\n\r\nActual behavior\r\n---------------\r\n\r\nPowerShell unwrapped the exception and gave me only what *caused* the exception. If I wanted that, I *would have* said so, since this works (and produces the same output as above)\r\n\r\n```powershell\r\ntry {\r\n   get-content nosuchfile -ErrorAction Stop\r\n} catch [System.Management.Automation.ItemNotFoundException] {\r\n   Write-Host \"It's an ItemNotFoundException, obviously\"\r\n   $_.PSTypeNames, $_.Exception.PSTypeNames, $_.Exception.InnerException.PSTypeNames\r\n} \r\n```\r\n\r\n## NOTE: THE REAL PROBLEM\r\n\r\nIt is *not* possible to catch the specific type explicitly, and **also** handle the generic case:\r\n\r\n```powershell\r\ntry {\r\n   get-content nosuchfile -ErrorAction Stop\r\n} catch [System.Management.Automation.ItemNotFoundException] {\r\n   Write-Host \"It's an ItemNotFoundException, which I can fix ... \"\r\n}  catch [System.Management.Automation.ActionPreferenceStopException] {\r\n   $_.PSTypeNames, $_.Exception.PSTypeNames, $_.Exception.InnerException.PSTypeNames\r\n} \r\n```\r\n\r\nEnvironment data\r\n----------------\r\n\r\nThis has *always* been a problem, and it persists in PowerShell 6.\r\n\r\n```powershell\r\n> $PSVersionTable\r\n\r\nName                           Value\r\n----                           -----\r\nPSVersion                      6.0.0\r\nPSEdition                      Core\r\nGitCommitId                    v6.0.0\r\nOS                             Microsoft Windows 10.0.15063\r\nPlatform                       Win32NT\r\nPSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}\r\nPSRemotingProtocolVersion      2.3\r\nSerializationVersion           1.1.0.1\r\nWSManStackVersion              3.0\r\n\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/PowerShell/PowerShell/issues/comments/373887380","html_url":"https://github.com/PowerShell/PowerShell/issues/6200#issuecomment-373887380","issue_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/6200","id":373887380,"user":{"login":"BrucePay","id":6876108,"avatar_url":"https://avatars3.githubusercontent.com/u/6876108?v=4","gravatar_id":"","url":"https://api.github.com/users/BrucePay","html_url":"https://github.com/BrucePay","followers_url":"https://api.github.com/users/BrucePay/followers","following_url":"https://api.github.com/users/BrucePay/following{/other_user}","gists_url":"https://api.github.com/users/BrucePay/gists{/gist_id}","starred_url":"https://api.github.com/users/BrucePay/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BrucePay/subscriptions","organizations_url":"https://api.github.com/users/BrucePay/orgs","repos_url":"https://api.github.com/users/BrucePay/repos","events_url":"https://api.github.com/users/BrucePay/events{/privacy}","received_events_url":"https://api.github.com/users/BrucePay/received_events","type":"User","site_admin":false},"created_at":"2018-03-17T02:18:33Z","updated_at":"2018-03-17T02:18:33Z","author_association":"MEMBER","body":"@Jaykul Yes - this behaviour that's been around since the very beginning of PowerShell. Basically the pipeline uses `ActionPreferenceStopException` to wrap the exception that has the actual error in it then throws the wrapper. Consequently, if it wasn't automatically unwrapped in try/catch/trap you would never be able to catch the underlying exception since all you'd ever see would be the wrapper. So your example:\r\n```\r\ntry {\r\n   get-content nosuchfile -ErrorAction Stop\r\n} catch [System.Management.Automation.ItemNotFoundException] {\r\n   Write-Host \"It's an ItemNotFoundException, obviously\"\r\n   $_.PSTypeNames, $_.Exception.PSTypeNames, $_.Exception.InnerException.PSTypeNames\r\n} \r\n```\r\nwouldn't work. \r\n\r\nWhy do you want to catch this exception? "}}
{"actor":{"display_login":"BrucePay"}}