Title: Why do handled exceptions show in ErrorVariable? · Issue #3768 · PowerShell/PowerShell · GitHub
Open Graph Title: Why do handled exceptions show in ErrorVariable? · Issue #3768 · PowerShell/PowerShell
X Title: Why do handled exceptions show in ErrorVariable? · Issue #3768 · PowerShell/PowerShell
Description: When you use try/catch and handle an exception, it still shows up in the global $Error, and more importantly, in the ErrorVariable. Is there any way to actually prevent handled exceptions from leaking into the ErrorVariable? Is there any...
Open Graph Description: When you use try/catch and handle an exception, it still shows up in the global $Error, and more importantly, in the ErrorVariable. Is there any way to actually prevent handled exceptions from leak...
X Description: When you use try/catch and handle an exception, it still shows up in the global $Error, and more importantly, in the ErrorVariable. Is there any way to actually prevent handled exceptions from leak...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/3768
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Why do handled exceptions show in ErrorVariable?","articleBody":"When you use try/catch and handle an exception, it still shows up in the global `$Error`, and _more importantly_, in the ErrorVariable.\r\n\r\nIs there any way to actually prevent handled exceptions from leaking into the ErrorVariable? Is there any way for a caller to determine that they are **not,** in fact, errors, but expected results that were dealt with by the function? That is, how does a caller distinguish the errors that matter?\r\n\r\nConsider a simple example:\r\n\r\n```posh\r\nfunction Count-Parameters {\r\n [CmdletBinding()]\r\n param([Parameter(ValueFromRemainingArguments)]$Parameters)\r\n try {\r\n $Parameters.FasterMethod()\r\n } catch {\r\n $Parameters.Count\r\n }\r\n}\r\n\r\n$Count = Count-Parameters # Everything is fine\r\n\r\n$Count = Count-Parameters This One Has Four # Still fine\r\n\r\n# But then my paranoid \"best practice\" fanatic does something like this:\r\n$Count = Count-Parameters -ErrorVariable noCount\r\nif(!$Count -and $NoCount) { \r\n Write-Warning $noCount\r\n}\r\n```\r\n\r\nHow can I stop the error variable from containing these things that I explicitly handled?","author":{"url":"https://github.com/Jaykul","@type":"Person","name":"Jaykul"},"datePublished":"2017-05-12T02:43:20.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":20},"url":"https://github.com/3768/PowerShell/issues/3768"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:41bc8b03-326e-2cc1-98b6-579aa0937429 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A594:D3A0E:1D0376F:2697F6D:6A5D50B7 |
| html-safe-nonce | 745276de11dcd326738760e3f9969fa981c963ad02985c1fb9f7caf1ed41b8cc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTk0OkQzQTBFOjFEMDM3NkY6MjY5N0Y2RDo2QTVENTBCNyIsInZpc2l0b3JfaWQiOiIzMTYwNjMxMzkyMjcyNzMyMzQzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1cf0a42cde79ef9318d2eb7fc5aafa2592b80ef5c8d9023063b8e85b08f9b563 |
| hovercard-subject-tag | issue:228173284 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/3768/issue_layout |
| twitter:image | https://opengraph.githubassets.com/05f46b4afc6c8ac9d74d89b1a0848ae8bff7f5f56533c1ccefe6635b6b7d2ff6/PowerShell/PowerShell/issues/3768 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/05f46b4afc6c8ac9d74d89b1a0848ae8bff7f5f56533c1ccefe6635b6b7d2ff6/PowerShell/PowerShell/issues/3768 |
| og:image:alt | When you use try/catch and handle an exception, it still shows up in the global $Error, and more importantly, in the ErrorVariable. Is there any way to actually prevent handled exceptions from leak... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Jaykul |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width