简短说明
描述生成终止错误的 throw 关键字。
详细说明
              throw 关键字会导致终止错误。 可以使用 throw 关键字停止处理命令、函数或脚本。
例如,可以在 throw 语句的脚本块中使用 if 关键字来响应条件或 catchtry-catch- 语句的 finally 块中。
              throw 关键字可以引发任何对象,例如用户消息字符串或导致错误的对象。
Syntax
              throw 关键字的语法如下所示:
throw [<expression>]
              throw 语法中的表达式是可选的。 如果 throw 语句未出现在 catch 块中,并且不包含表达式,则会生成 ScriptHalted 错误。
throw
Exception: ScriptHalted
如果在没有表达式的 throw 块中使用 catch 关键字,它将再次引发当前 RuntimeException。 有关详细信息,请参阅 about_Try_Catch_Finally。
引发字符串
              throw 语句中的可选表达式可以是字符串,如以下示例所示:
throw "This is an error."
Exception: This is an error.
引发其他对象
表达式也可以是引发表示 PowerShell 进程的对象的对象,如以下示例所示:
throw (Get-Process pwsh)
Exception: System.Diagnostics.Process (pwsh) System.Diagnostics.Process (pwsh) System.Diagnostics.Process (pwsh)
可以使用  自动变量中 ErrorRecord 对象的 $Error 属性来检查错误。
$Error[0].TargetObject
 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
    125   174.44     229.57      23.61    1548   2 pwsh
     63    44.07      81.95       1.75    1732   2 pwsh
     63    43.32      77.65       1.48    9092   2 pwsh
还可以 throwErrorRecord 对象或 .NET 异常。 以下示例使用 throw 关键字引发 System.FormatException 对象。
$formatError = New-Object System.FormatException
throw $formatError
OperationStopped: One of the identified items was in an invalid format.
生成的错误
              throw 关键字可以生成 ErrorRecord 对象。 
              ErrorRecord 对象的 Exception 属性包含 RuntimeException 对象。
              ErrorRecord 对象的其余部分和 RuntimeException 对象因引发的对象而异。
              throw 对象包装在 ErrorRecord 对象中,ErrorRecord 对象自动保存在 $Error 自动变量中。
用于 throw 创建必需参数
与以前版本的 PowerShell 不同,请勿使用 throw 关键字进行参数验证。 有关正确方法,请参阅 about_Functions_Advanced_Parameters。