Throw throws a script -terminating (run-terminating) error , while $PScmdlet.ThrowTerminatingError() statement termination error .
Note: These are not official terms (documents currently only vaguely refer to final errors in an abstract text, without scope), but they are useful for describing de facto behavior. p>
In short: default
A script termination error terminates the entire workspace , i.e. the script works and all its callers, without any further statements being executed.
- The only way to intercept them is to use the
try / catch operator (or, as a rule, the trap operator).
while the statement finalizing error completes only the current statement (the function that calls $PScmdlet.ThrowTerminatingError() , and the statement in which it enters, which is often a pipeline), and execution continues with the next statement.
They can also be intercepted using try / catch (or trap ) or ignored using the $ErrorActionPreference preference variable set to SilentlyContinue ; on the contrary, the general parameter -ErrorAction does not affect them.
Conversely, you can advertise them to script-terminal errors with $ErrorActionPreference = 'Stop' , despite the fact that documents claiming that $ErrorActionPreference refers only to non-exhaustive errors are three types of errors.
For more information, see this (unofficial) PowerShell error handling overview .
As a guide, when to use the error type :
A small guide to using Throw in the about_Throw reference topic; An example of use is given in which Throw used to interrupt the / script function in the absence of a required parameter as an alternative to the default behavior for a PowerShell request.
- Just keep in mind that
Throw , i.e. throwing a script termination error terminates the entire work area (a running script and any of its callers), unless caught.
The error message with the error message in the article discusses the internal use of cmdlet with respect to when to report the completion of output (referred to simply as "finishing" in the article) against errors without interruption .
- A brief review of the article can be found in this answer .
Given the latter, you can argue that advanced functions, since they are similar to cmdlets, should handle errors related to report completion as much as possible, and that Throw (script -terminating) errors should be limited to scripts, but note that it is contrary to using Throw for forced application of required parameters.
Perhaps the problematic difference between script termination and termination errors was ultimately unintentional, and perhaps the original intention was to have only script terminal, which explains why all the current documentation only ever talks about ending up in errors abstract without even mentioning the differences.
source share