主題
    about_Throw

簡短描述
    說明產生終止錯誤的 Throw 關鍵字。

完整描述
    Throw 關鍵字會造成終止錯誤。您可以使用 Throw 關鍵字停止處理命令、函數或指
    令碼。

    例如,您可以在 if 陳述式的指令碼區塊中,或是在 Try-Catch-Finally 陳述式的
    Catch 區塊中使用 Throw 關鍵字回應條件。您也可以在參數宣告中使用 Throw 關
    鍵字,讓函數參數成為必要參數。

    Throw 關鍵字會擲回任何物件,例如使用者訊息字串,或是造成錯誤的物件。


 語法
    Throw 關鍵字的語法如下:

        throw [<運算式>]


    Throw 語法中的運算式是選擇性的。當 Throw 陳述式未出現在 Catch 區塊中,且未
    包含運算式時,會產生 ScriptHalted 錯誤。

        C:\PS> throw

        ScriptHalted
        位於第 1 行,第 6 個字元
        + throw <<<<
            + CategoryInfo          : OperationStopped: (:) [], RuntimeException 
            + FullyQualifiedErrorId : ScriptHalted


    如果在沒有運算式的 Catch 區塊中使用 Throw 關鍵字,則會再次擲回目前的
    RuntimeException。如需詳細資訊,請參閱 about_Try_Catch_Finally。


 擲回字串

    Throw 陳述式中的選擇性運算式可以是字串,如下列範例所示:

        C:\PS> throw "這是錯誤。"

        這是錯誤。
        位於第 1 行,第 6 個字元
        + throw <<<< "這是錯誤。"
            + CategoryInfo: OperationStopped: (這是錯誤。:String) [], RuntimeException
            + FullyQualifiedErrorId : 這是錯誤。


 擲回其他物件

    運算式也可以是物件,此物件會擲回代表 PowerShell 處理序的物件,如下列範例所
    示:

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

        System.Diagnostics.Process (powershell) 位於第 1 行,第 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 物件或 Microsoft .NET Framework 例外狀況。下列範
    例會使用 Throw 關鍵字擲回 System.FormatException 物件。

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

        C:\PS> throw $formatError

        辨識項目的其中之一使用無效的格式。
        位於第 1 行,第 6 個字元
        + throw <<<< $formatError
            + CategoryInfo          : OperationStopped: (:) [], FormatException 
            + FullyQualifiedErrorId : 辨識項目的其中之一使用無效的格式。


 導致錯誤

    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 "必須有 Path 參數。")) 
            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




目錄