主题
    about_Throw

简短说明
    介绍用于生成终止错误 Throw 关键字。

详细说明
    Throw 关键字可生成终止错误。可以使用 Throw 关键字来停止命令、函数或脚本的执行。

    例如,可在 If 语句的脚本块中使用 Throw 关键字对某个条件做出响应,也可在 Try-Catch-Finally 语句的 
    Catch 块中使用该关键字。另外,还可在参数声明中使用 Throw 关键字将某个函数参数设为必需参数。

    Throw 关键字可引发任何对象,如用户消息字符串或导致错误的对象。


 语法
    Throw 关键字的语法如下:

        throw [<expression>]


    Throw 语法中的表达式为可选项。如果 Throw 语句并不是在 Catch 块中出现,并且不包括表达式,那么它将生
    成 ScriptHalted 错误。

        C:\PS> throw

        ScriptHalted
        At line:1 char:6
        + throw <<<<
            + CategoryInfo          : OperationStopped: (:) [], RuntimeException
            + FullyQualifiedErrorId : ScriptHalted


    如果在 Catch 块中使用 Throw 关键字且不带表达式,它将再次引发当前 RuntimeException。有关详细信
    息,请参阅 about_Try_Catch_Finally。


 引发字符串
    Throw 语句中的可选表达式可以是一个字符串,如以下示例所示:

        C:\PS> throw "This is an error."

        This is an error.
        At line:1 char:6
        + throw <<<<  "This is an error."
            + CategoryInfo          : OperationStopped: (This is an error.:String) [], RuntimeException
            + FullyQualifiedErrorId : This is an error.


 引发其他对象
    该表达式也可以是对象,如以下示例所示,它引发表示 PowerShell 进程的对象。

        C:\PS> throw (get-process powershell)

        System.Diagnostics.Process (powershell)
        At line:1 char:6
        + throw <<<<  (get-process powershell)
            + CategoryInfo          : OperationStopped: (System.Diagnostics.Process (powershell):Process) [], 
        				RuntimeException
            + FullyQualifiedErrorId : System.Diagnostics.Process (powershell)


    可使用自动变量 $error 中 ErrorRecord 对象的 TargetObject 属性来检查错误。


        C:\PS> $error[0].targetobject

        Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                            
        -------  ------    -----      ----- -----   ------     -- -----------                                                            
            319      26    61016      70864   568     3.28   5548 powershell
    

    还可引发 ErrorRecord 对象或 .NET Framework 异常。以下示例使用 Throw 关键字引发一个 
    System.FormatException 对象。

        C:\PS> $formatError = new-object system.formatexception

        C:\PS> throw $formatError

        One of the identified items was in an invalid format.
        At line:1 char:6
        + throw <<<<  $formatError
            + CategoryInfo          : OperationStopped: (:) [], FormatException
            + FullyQualifiedErrorId : One of the identified items was in an invalid format.


 产生的错误
    Throw 关键字可生成 ErrorRecord 对象。ErrorRecord 对象的 Exception 属性包含一个 
    RuntimeException 对象。ErrorRecord 对象和 RuntimeException 对象的其余属性随 Throw 关键字所
    引发对象的不同而有所区别。

    RunTimeException 对象包装在 ErrorRecord 对象中,ErrorRecord 对象自动保存在 $Error 自动变量中。


 使用 Throw 创建必需参数
    可使用 Throw 关键字将某个函数参数设为必需参数。

    这是使用 Parameter 关键字的 Mandatory 参数的替代方法。使用 Mandatory 参数时,系统会提示用户输入
    必需的参数值。使用 Throw 关键字时,该命令会停止并会显示错误记录。

    例如,参数子表达式中的 Throw 关键字将使 Path 参数成为函数中的必需参数。

    在下面的例子中,Throw 关键字引发一个消息字符串,但正是因为存在 Throw 关键字,才会在未指定 Path 参数
    时生成终止错误。Throw 后的表达式为可选项。

        function Get-XMLFiles
        {
            param ($path = $(throw "The Path parameter is required."))
            dir -path $path\* -include *.xml -recurse | sort lastwritetime | ft lastwritetime, attributes, name  -auto
        }


另请参阅
    about_Break
    about_Continue
    about_Scope
    about_Trap
    about_Try_Catch_Finally




目录