Title: Promoting a statement-terminating error to a script-terminating one results in unhelpful $Error entry. · Issue #4837 · PowerShell/PowerShell · GitHub
Open Graph Title: Promoting a statement-terminating error to a script-terminating one results in unhelpful $Error entry. · Issue #4837 · PowerShell/PowerShell
X Title: Promoting a statement-terminating error to a script-terminating one results in unhelpful $Error entry. · Issue #4837 · PowerShell/PowerShell
Description: Note: The behavior may be an indication that there never should have been statement-terminating errors to begin with, only script-terminating ones, as reflected in the current documentation neglecting to make this important distinction. ...
Open Graph Description: Note: The behavior may be an indication that there never should have been statement-terminating errors to begin with, only script-terminating ones, as reflected in the current documentation neglect...
X Description: Note: The behavior may be an indication that there never should have been statement-terminating errors to begin with, only script-terminating ones, as reflected in the current documentation neglect...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/4837
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Promoting a statement-terminating error to a script-terminating one results in unhelpful $Error entry.","articleBody":"Note: The behavior may be an indication that there never should have been _statement_-terminating errors to begin with, only _script_-terminating ones, as reflected in [the current documentation neglecting to make this important distinction](https://github.com/PowerShell/PowerShell-Docs/issues/1583).\r\n\r\nIn practice, an (uncaught) statement-terminating error (e.g., `1 / 0` or `Get-Item -NoSuchParam`) does _not_ terminate the script, but _can_ be made to do so via `$ErrorActionPreference = 'Stop'`, against documented behavior (but _not_ with `-ErrorAction Stop`).\r\n\r\nIf you do, you get:\r\n\r\n* the usual error record reflecting the statement-terminating error in `$Error`\r\n* an _additional, unhelpful_ `$Error` entry that is simply a _part_ of the statement-terminating-error record, namely the _underlying exception_.\r\n\r\nIn other words: you get _2_ `$Error` entries, with the exception-only entry coming after (reflected in `$Error[0]` immediately after, with `$Error[1]` containing the statement-terminating-error record.\r\n\r\nThe exact relationship is:\r\n\r\n```powershell\r\n[object]::ReferenceEquals($Error[1].exception, $Error[0]) # -> $True\r\n```\r\n\r\nContrast this with promoting a _non-terminating_ error to a script-terminating one, where you also get _2_ entries, but the extra entry is a _helpful_ exception (`[System.Management.Automation.ActionPreferenceStopException]`) whose message reflects the fact of the promotion:\r\n\r\n```powershell\r\n# Promote a non-terminating error to a script-terminating one.\r\n> $Error.Clear(); Get-Item -ErrorAction Stop /NoSuchItem\r\nGet-Item : Cannot find path '/NoSuchItem' because it does not exist.\r\n...\r\n# Print the message of the extra $Error entry that was recorded.\r\n# (exception System.Management.Automation.ActionPreferenceStopException)\r\n> $Error[0].Message\r\nThe running command stopped because the preference variable \"ErrorActionPreference\" or common parameter is set to Stop: Cannot find path '/NoSuchItem' because it does not exist.\r\n```\r\n\r\n\r\nSteps to reproduce\r\n------------------\r\n\r\n```powershell\r\n$Error.Clear(); $ErrorActionPreference = 'stop'; Get-Item -NoSuchSwitch\r\n'---'\r\n$Error | % GetType | % Name\r\n```\r\n\r\nExpected behavior\r\n-----------------\r\n\r\nI'm describing expected behavior from the perspective of consistency with how things work with _non-terminating_ errors:\r\n\r\n```none\r\nGet-Item : A parameter cannot be found that matches parameter name 'NoSuchSwitch'.\r\n...\r\n---\r\nActionPreferenceStopException # indicate promotion to script-terminating error\r\nErrorRecord\r\n```\r\n\r\nActual behavior\r\n---------------\r\n\r\n```none\r\nGet-Item : A parameter cannot be found that matches parameter name 'NoSuchSwitch'.\r\n...\r\n---\r\nParameterBindingException # !! Simply duplicated the .Exception value of $Error[1]\r\nErrorRecord\r\n```\r\n\r\nEnvironment data\r\n----------------\r\n\r\n\u003c!-- provide the output of $PSVersionTable -->\r\n\r\n```powershell\r\nPowerShell Core v6.0.0-beta.6 on macOS 10.12.6\r\nPowerShell Core v6.0.0-beta.6 on Ubuntu 16.04.3 LTS\r\nPowerShell Core v6.0.0-beta.6 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)\r\nWindows PowerShell v5.1.15063.502 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2017-09-14T17:39:56Z","url":"https://github.com/PowerShell/PowerShell/issues/4837"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:d37b5bc6-5b70-3fe5-5928-fad305007f03 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AF2E:268AD4:219E67:2DEE7B:6AFD51BE |
| html-safe-nonce | 2e6d7f52272b3a868c8e0f8a9dc78b23fb0b099a3dacec723c5465ebf4965cad |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjJFOjI2OEFENDoyMTlFNjc6MkRFRTdCOjZBRkQ1MUJFIiwidmlzaXRvcl9pZCI6IjM2ODMyNjA2MjM4MzY0MDk0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | b03c69115b2cf8f4e4a863f99923d4fb8e46838d4435d934d00151b3dac37ee4 |
| hovercard-subject-tag | issue:257800574 |
| 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/4837/issue_layout |
| twitter:image | https://opengraph.githubassets.com/231f77d9312083be2836d4c4205f7523610c9503fb756620820ac03afde01f51/PowerShell/PowerShell/issues/4837 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/231f77d9312083be2836d4c4205f7523610c9503fb756620820ac03afde01f51/PowerShell/PowerShell/issues/4837 |
| og:image:alt | Note: The behavior may be an indication that there never should have been statement-terminating errors to begin with, only script-terminating ones, as reflected in the current documentation neglect... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 66653c2e55a6fff2930be1bdf07ee4f8aa137e3956d056c822563be6f878755e |
| 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 | b6f01f7cb9f35f8c09949773241a3d49dd04b340 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width