You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Provide a way to distinguish between terminating error vs. non-terminating error #3158
To distinguish errors as they occur, use a Try / Catch statement: only terminating errors will trigger the Catch handler:
Note: I'm using 1 / 0 as a stand-in for a pipeline-terminating error here:
# Trigger a pipeline-terminating error; works the same with a script-terminating error (Throw).$errCountBefore=$Error.Count$terminatingErrorOccurred=$FalseTry {
1/0
} Catch { # TRIGGERED by a TERMINATING ERROR - both pipeline-terminating and script-terminating# Note: To also *output* the error, use Write-Error $_, but note that this# *duplicates* the automatically generated $Error entry.Write-Error$_$terminatingErrorOccurred=$True
}
if ($terminatingErrorOccurred) { 'Terminating error occurred.' }
elseif ($Error.Count-gt$errCountBefore) { 'Non-terminating error occurred.' }
'---'# Trigger a non-terminating error.$errCountBefore=$Error.Count$terminatingErrorOccurred=$FalseTry {
# A non-terminating error prints its output to the error stream,# irrespective of any enclosing Try/Catch handler.Get-Item/NoSuch
} Catch { # NOT triggered by a NON-TERMINATING ERROR$terminatingErrorOccurred=$True
}
if ($terminatingErrorOccurred) { 'Terminating error occurred.' }
elseif ($Error.Count-gt$errCountBefore) { 'Non-terminating error occurred.' }
The above yields:
/Users/jdoe/Desktop/pg/pg1.ps1 : Attempted to divide by zero.
....
Terminating error occurred.
---
Get-Item : Cannot find path '/NoSuch' because it does not exist.
...
Non-terminating error occurred.
Activity
apetitjean commentedon Feb 23, 2017
That is definitely something that should be addressed! That makes just 10 years we struggle with that ;-)
mklement0 commentedon Jul 19, 2017
Related: #4292
Given the current capabilities:
In hindsight, by inspecting
$Error, I don't think there's a (straightforward) way to classify previous errors - though I'm not sure that's needed. [Update: It is needed in order to provide error type-specific coloring, for instance - see Suggestion: Add an $ErrorView option that displays concise, single-line error information and consider making that the default - default formatting of ErrorRecord #3647]To distinguish errors as they occur, use a
Try / Catchstatement: only terminating errors will trigger theCatchhandler:Note: I'm using
1 / 0as a stand-in for a pipeline-terminating error here:The above yields:
joeyaiello commentedon Mar 13, 2019
Steve Lee (@SteveL-MSFT): I want this for Functions per our discussion on
criticalvserrorlogs./cc TylerLeonhardt Dongbo Wang (@daxian-dbw)
SteveL-MSFT commentedon Mar 15, 2019
Dupe of #4781. Yes, this one is older, but the latter has more discussion.