throw和$ pscmdlet.ThrowTerminatederror()之间的区别? [英] Difference between throw and $pscmdlet.ThrowTerminatingerror()?
问题描述
在PowerShell中,throw $ErrorMsg
和$PScmdlet.ThrowTerminatingError($ErrorMsg)
有什么区别?
In PowerShell, what is the difference between throw $ErrorMsg
and $PScmdlet.ThrowTerminatingError($ErrorMsg)
?
它们是相同还是不同?如果它们不同,哪个更可取?
Are they same or different? If they are different which one is preferable?
推荐答案
Throw
创建一个脚本终止(运行空间终止)错误,而 $PScmdlet.ThrowTerminatingError()
创建一个声明终止错误.
Throw
creates a script-terminating (runspace-terminating) error, whereas $PScmdlet.ThrowTerminatingError()
creates a statement-terminating error.
注意:这些不是官方术语(文档目前仅含糊地引用了摘要中的终止错误,没有范围),但是它们对于描述事实行为.
Note: These aren't official terms (the docs currently only vaguely reference terminating errors in the abstract, without scope), but they're useful for describing the de-facto behavior.
简而言之:默认情况下,
In short: By default,
-
脚本终止错误会终止整个运行空间,即正在运行的脚本及其所有调用方,不再执行任何其他操作语句被执行.
a script-terminating error terminates the entire runspace, i.e., the running script and all its callers, with no further statements getting executed.
- 仅 拦截它们的方法是使用
try
/catch
语句(或者,较少见的是trap
语句).
- The only way to intercept them is by using a
try
/catch
statement (or, less commonly, atrap
statement).
,而语句 终止错误仅终止当前语句(调用$PScmdlet.ThrowTerminatingError()
的函数及其声明是的一部分,通常是 pipeline ),执行会继续执行 next 语句.
whereas a statement-terminating error terminates only the current statement (the function calling $PScmdlet.ThrowTerminatingError()
and the statement it is a part of, which is often a pipeline), with execution continuing with the next statement.
-
它们也可以被
try
/catch
(或trap
)拦截,或者被忽略并将$ErrorActionPreference
首选项变量设置为SilentlyContinue
;相比之下,-ErrorAction
通用参数对它们没有 作用.
They too can be intercepted with
try
/catch
(ortrap
), or ignored with the$ErrorActionPreference
preference variable set toSilentlyContinue
; by contrast, the-ErrorAction
common parameter has no effect on them.
相反,尽管文档声称$ErrorActionPreference
仅与 nonterminate 错误有关,但是您可以通过$ErrorActionPreference = 'Stop'
promote 将其终止为脚本终止错误,第三种错误类型.
Conversely, you can promote them to script-terminating errors with $ErrorActionPreference = 'Stop'
, despite the docs claiming that $ErrorActionPreference
pertains only to nonterminating errors, the 3rd error type.
有关更多信息,请参见 PowerShell错误处理的此(非正式)概述.
For more information, see this (unofficial) overview of PowerShell's error handling.
关于何时使用哪种错误类型的指南:
There is little guidance as to when to use
Throw
in theabout_Throw
help topic; an example use case is given in whichThrow
is used to abort a function/script in the absence of a mandatory parameter, as an alternative to PowerShell's default behavior of prompting for it.
- 请注意,除非捕获到
Throw
,即抛出 script 终止错误会终止整个运行空间(正在运行的脚本及其任何调用程序).
- Just be aware that
Throw
, i.e., throwing a script-terminating error terminates the entire runspace (the running script and any of its callers), unless caught.
Cmdlet错误报告文章仅讨论 cmdlet-内部 在报告声明终止时(简称终止")时的使用情况. ;在文章中)与 nonterminate 错误.
The Cmdlet Error Reporting article only discusses cmdlet-internal use with respect to when to report statement-terminating (called just "terminating" in the article) vs. nonterminating errors.
- 本文的简要摘要可在我的此答案中找到.
- A concise summary of the article can be found in this answer of mine.
鉴于后者,您可能会说高级功能-因为它们就像cmdlet-最多应 报告声明终止错误并且Throw
(脚本终止错误)应仅限于脚本,但请注意,这与Throw
强制使用强制性参数相矛盾.
Given the latter, you could argue that advanced functions - since they're like cmdlets - should at most report statement-terminating errors and that Throw
(script-terminating errors) should be limited to scripts, but note that that contradicts the use of Throw
to enforce mandatory parameters.
也许 脚本终止和陈述终止错误之间的问题区别最终是无意和也许最初的意图是只包含 script 终止符,这可以解释为什么所有当前文档只抽象地提及终止错误,甚至没有提及区别.
Perhaps the problematic distinction between script-terminating and statement-terminating errors is ultimately unintentional and perhaps the original intent was to only ever have script-terminating ones, which would explain why all the current documentation only ever talks about terminating errors in the abstract, without even mentioning the distinction.
这篇关于throw和$ pscmdlet.ThrowTerminatederror()之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!