{"action":"opened","issue":{"url":"https://api.github.com/repos/AArnott/pinvoke/issues/199","repository_url":"https://api.github.com/repos/AArnott/pinvoke","labels_url":"https://api.github.com/repos/AArnott/pinvoke/issues/199/labels{/name}","comments_url":"https://api.github.com/repos/AArnott/pinvoke/issues/199/comments","events_url":"https://api.github.com/repos/AArnott/pinvoke/issues/199/events","html_url":"https://github.com/AArnott/pinvoke/issues/199","id":136202065,"number":199,"title":"HResult is missing a method to get an exception from it","user":{"login":"vbfox","id":131878,"avatar_url":"https://avatars.githubusercontent.com/u/131878?v=3","gravatar_id":"","url":"https://api.github.com/users/vbfox","html_url":"https://github.com/vbfox","followers_url":"https://api.github.com/users/vbfox/followers","following_url":"https://api.github.com/users/vbfox/following{/other_user}","gists_url":"https://api.github.com/users/vbfox/gists{/gist_id}","starred_url":"https://api.github.com/users/vbfox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vbfox/subscriptions","organizations_url":"https://api.github.com/users/vbfox/orgs","repos_url":"https://api.github.com/users/vbfox/repos","events_url":"https://api.github.com/users/vbfox/events{/privacy}","received_events_url":"https://api.github.com/users/vbfox/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/AArnott/pinvoke/labels/enhancement","name":"enhancement","color":"84b6eb"},{"url":"https://api.github.com/repos/AArnott/pinvoke/labels/needs%20discussion","name":"needs discussion","color":"fbca04"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-02-24T22:12:19Z","updated_at":"2016-02-24T22:12:19Z","closed_at":null,"body":"We already have `ThrowOnFailure` calling `Marshal.ThrowExceptionForHR` but don't have any equivalent to `Marshal.GetExceptionForHR`.\r\n\r\n````csharp\r\nFoo Function()\r\n{\r\n    Foo result;\r\n    HResult hr = SomePInvoke(out result);\r\n    if (hr.Succeeded)\r\n    {\r\n        return result;\r\n    }\r\n    else\r\n    {\r\n        if (hr == /*Some value*/)\r\n        {\r\n            throw MyError();\r\n        }\r\n        throw hr.GetException();\r\n    }\r\n}\r\n```\r\n\r\nIt's often possible to do without but it's sometimes preferable to have explicit `throw` for control-flow and it ease porting code using the `Mashal` class"}}