about_Trap

適用於: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

主題

about_Trap

簡短描述

描述處理終止錯誤的關鍵字。

詳細描述

終止錯誤會導致陳述式停止執行。如果 Windows PowerShell® 因某種因素而無法處理終止錯誤,則 Windows PowerShell 也會停止目前管線中執行的函式或指令碼。在像是 C# 的其他語言中,終止錯誤稱為例外狀況。

Trap 關鍵字會指定在終止錯誤發生時要執行的陳述式清單。Trap 陳述式會處理終止錯誤並允許繼續執行 (而非停止) 指令碼或函式。

語法

Trap 陳述式的語法如下:

          trap [[<error type>]] {<statement list>}

Trap 陳述式包含在終止錯誤發生時要執行的陳述式清單。Trap 關鍵字可以選擇性地指定錯誤類型。錯誤類型需以方括號括住。

指令碼或命令可以有多個 Trap 陳述式。Trap 陳述式可以出現在指令碼或命令中的任何位置。

設陷所有終止錯誤

當終止錯誤發生且未在指令碼或命令中以另一種方式處理時,Windows PowerShell 便會檢查處理錯誤的 Trap 陳述式。如果 Trap 陳述式存在,則 Windows PowerShell 會繼續在 Trap 陳述式中執行指令碼或命令。

下列範例是一個非常簡單的 Trap 陳述式:

          trap {"Error found."}

這個 Trap 陳述式會設陷任何終止錯誤。下列範例是包含此 Trap 陳述式的函式:

          function TrapTest {
              trap {"Error found."}
              nonsenseString
              }

此函式包括會造成錯誤的無意義字串。執行這個函式會傳回下列結果:

          C:\PS> TrapTest
          Error found.

下列範例包含使用 $_ 自動變數就會顯示錯誤的 Trap 陳述式:

          function TrapTest {
              trap {"Error found: $_"}
              nonsenseString
              }

執行此版本的函式會傳回下列結果:

          C:\PS> TrapTest
          Error found: The term 'nonsenseString' is not recognized as the name 
          of a cmdlet, function, script file, or operable program. Check the 
          spelling of the name, or if a path was included verify that the path 
          is correct, and then try again.

Trap 陳述式也可以更複雜。Trap 陳述式可包含多個條件或函式呼叫。它可以記錄、測試,甚至執行另一個程式。

設陷指定的終止錯誤

下列範例是設陷 CommandNotFoundException 錯誤類型的 Trap 陳述式:

          trap [System.Management.Automation.CommandNotFoundException] 
              {"Command error trapped"}

當函式或指令碼遇到不符合已知命令的字串時,則此 Trap 陳述式會顯示「已設陷命令錯誤」字串。執行任何在 Trap 陳述式清單中的陳述式之後,Windows PowerShell 會將錯誤物件寫入錯誤資料流,然後繼續執行指令碼。

Windows PowerShell 使用 Microsoft .NET Framework 例外狀況類型。下列範例指定 System.Exception 錯誤類型:

          trap [System.Exception] {"An error trapped"}

CommandNotFoundException 錯誤類型繼承自 System.Exception 類型。這個陳述式會設陷由未知命令所建立的錯誤。它也會設陷其他錯誤類型。

您在一個指令碼中可以有一個以上的 Trap 陳述式。每個錯誤只能被一個 Trap 陳述式設陷。如果錯誤發生且有多個 Trap 陳述式可用時,Windows PowerShell 會使用特定錯誤類型與該錯誤最相符的 Trap 陳述式。

下列指令碼範例包含一個錯誤。指令碼包含會設陷任何終止錯誤的一般 Trap 陳述式,和針對 CommandNotFoundException 類型的特定 Trap 陳述式。

          trap {"Other terminating error trapped" }
          trap [System.Management.Automation.CommandNotFoundException] {"Command error trapped"}
          nonsenseString

執行這個指令碼會產生下列結果:

          Command  error trapped
          The term 'nonsenseString' is not recognized as the name of a cmdlet, 
          function, script file, or operable program. Check the spelling of 
          the name, or if a path was included verify that the path is correct,
          and then try again.
          At C:\PS>testScript1.ps1:3 char:19
          +     nonsenseString <<<<

因為 Windows PowerShell 無法將 "nonsenseString" 辨識為 Cmdlet 或其他項目,因此其會傳回 CommandNotFoundException 錯誤。此終止錯誤會由特定的 Trap 陳述式設陷。

下列指令碼範例包含具有不同錯誤的相同 Trap 陳述式:

          trap {"Other terminating error trapped" }
          trap [System.Management.Automation.CommandNotFoundException] 
              {"Command error trapped"}
          1/$null

執行這個指令碼會產生下列結果:

          Other terminating error trapped
          Attempted to divide by zero.
          At C:PS> errorX.ps1:3 char:7
          +     1/ <<<< $null

嘗試以零相除並不會產生 CommandNotFoundException 錯誤。相反地,該錯誤會由其他設陷任何終止錯誤的 Trap 陳述式設陷。

設陷錯誤和範圍

如果終止錯誤發生在與 Trap 陳述式的相同範圍內,則在執行 Trap 陳述式之後,Windows PowerShell 就會繼續執行錯誤之後的陳述式。如果 Trap 陳述式位在與錯誤不同的範圍時,則與 Trap 陳述式位於相同範圍的下一個陳述式會繼續執行。

比方說,如果函式中發生錯誤,而 Trap 陳述式位於函式中,則指令碼會繼續執行下一個陳述式。例如,下列指令碼包含一個錯誤和 Trap 陳述式:

          function function1 {
              trap { "An error: " }
              NonsenseString
              "function1 was completed"
              }

在指令碼稍後的部分,執行 Function1 函式會產生下列結果:

          function1
          An error: 
          The term 'NonsenseString' is not recognized as the name of a cmdlet, 
          function, script file, or operable program. Check the spelling of the 
          name, or if a path was included verify that the path is correct, and 
          then try again.
          At C:\PS>TestScript1.ps1:3 char:19
          +     NonsenseString <<<<

          function1 was completed

函式中的 Trap 陳述式會設陷錯誤。顯示訊息之後,Windows PowerShell 會繼續執行函式。請注意 Function1 已經完成。

請將這一點與下列具有相同錯誤和 Trap 陳述式的範例做比較。在此範例中,Trap 陳述式發生在函式之外:

          function function2 {
              NonsenseString
              "function2 was completed"
              }

          trap { "An error: " }
              . . .
          function2

在指令碼稍後的部分,執行 Function2 函式會產生下列結果:

          An error: 
          The term 'NonsenseString' is not recognized as the name of a cmdlet, 
          function, script file, or operable program. Check the spelling of the 
          name, or if a path was included verify that the path is correct, and 
          then try again.
          At C:\PS>TestScript2.ps1:4 char:19
          +     NonsenseString <<<<

在此範例中,「function2 已完成」的命令並未執行。雖然這兩個終止錯誤都發生在函式中,但如果 Tarp 陳述式是外部函數,則 Windows PowerShell 在 Trap 陳述式執行之後不會回到函式中。

使用 BREAK 和 CONTINUE 關鍵字

您可以在 Tarp 陳述式中使用 Break 和 Continue 關鍵字來判斷是否有指令碼或命令在發生終止錯誤之後繼續執行。

如果您將 Break 陳述式包含在 Trap 陳述式清單中,則 Windows PowerShell 會停止函式或指令碼。下列範例函數會在 Tarp 陳述式中使用 Break 關鍵字:

          C:\PS> function break_example {
              trap {"Error trapped"; break;}
              1/$null
              "Function completed."
              }

          C:\PS> break_example
          Error trapped
          Attempted to divide by zero.
          At line:4 char:7

因為 Tarp 陳述式包含 Break 關鍵字,因此函式不會繼續執行,也不會執行「函式已完成」的那一行。

如果您在 Trap 陳述式中包含 Continue 陳述式,則 Windows PowerShell 會在造成錯誤的陳述式後面繼續執行,就像沒有 Break 或 Continue 時一樣。就算有了 Continue 關鍵字,Windows PowerShell 也不會將錯誤寫入錯誤資料流。

下列範例函數會在 Trap 陳述式中使用 Continue 關鍵字:

          C:\PS> function continue_example {
              trap {"Error trapped"; continue;}
              1/$null
              "Function completed."}

          C:\PS> continue_example
          Error trapped
          Function completed.

函式會在將錯誤設陷之後繼續,而「函式已完成」陳述式也會執行。沒有任何錯誤會寫入錯誤資料流。

另請參閱

about_Break

about_Continue

about_Scopes

about_Throw

about_Try_Catch_Finally