共用方式為


關於陷阱

簡短描述

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

完整描述

終止錯誤會停止語句執行。 如果 PowerShell 未以某種方式處理終止錯誤,PowerShell 也會停止在目前管線中執行函式或腳本。 在其他語言中,例如 C#,終止錯誤稱為例外狀況。

trap關鍵詞會指定要在終止錯誤發生時執行的語句清單。 trap 語句會以下列方式處理終止錯誤:

  • 在處理 trap 語句區塊后顯示錯誤,並繼續執行包含的 trap腳本或函式。 這是預設行為。

  • 顯示文稿或trap函式在語句中包含 trap using break 的錯誤和中止執行。

  • 讓錯誤無聲,但在語句中使用 繼續執行包含 的 trapcontinuetrap 腳本或函式。

trap 語句清單可以包含多個條件或函數調用。 trap可以寫入記錄、測試條件,或甚至執行另一個程式。

Syntax

trap 陳述式具有下列語法:

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

trap語句包含終止錯誤發生時所要執行的語句清單。 trap語句包含 trap 關鍵詞,選擇性地後面接著類型表達式,以及語句區塊,其中包含在截獲錯誤時所要執行的語句清單。 類型表達式會精簡攔截的錯誤 trap 類型。

腳本或命令可以有多個 trap 語句。 trap 語句可以出現在腳本或命令中的任何位置。

截獲所有終止錯誤

當文稿或命令中未以另一種方式處理終止錯誤時,PowerShell 會檢查 trap 處理錯誤的語句。 trap如果語句存在,PowerShell 會繼續在 語句中trap執行腳本或命令。

下列範例是非常簡單 trap 的語句:

trap {"Error found."}

trap 語句會捕捉任何終止錯誤。

在下列範例中,函式包含造成運行時間錯誤的非字串。

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

TrapTest

執行此函式會傳回下列專案:

Error found.
nonsenseString:
Line |
   3 |      nonsenseString
     |      ~~~~~~~~~~~~~~
     | 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 try again.

下列範例包含 trap 語句,此語句會使用 $_ 自動變數來顯示錯誤:

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

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 try again.
nonsenseString:
Line |
   3 |      nonsenseString
     |      ~~~~~~~~~~~~~~
     | 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 try again.

重要

trap 語句可以在指定範圍內的任何位置定義,但一律會套用至該範圍內的所有語句。 在運行時間, trap 區塊中的語句會在執行任何其他語句之前定義。 在 JavaScript 中,這稱為 擷取。 這表示語句 trap 會套用至該區塊中的所有語句,即使執行尚未超過定義它們的時間點也一樣。 例如,在文稿結尾定義 trap ,並在第一個語句中擲回錯誤仍會觸發該 trap

截獲特定錯誤

腳本或命令可以有多個 trap 語句。 trap可以定義 來處理特定錯誤。

下列範例是攔截 trap 特定錯誤 CommandNotFoundException 的語句:

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

當函式或腳本遇到不符合已知命令的字串時,此 trap 語句會顯示「截獲命令錯誤」字串。 執行 trap 語句清單之後,PowerShell 會將錯誤物件寫入錯誤數據流,然後繼續腳本。

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

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

CommandNotFoundException 錯誤類型繼承自 System.Exception 類型。 此語句會捕捉未知命令所建立的錯誤。 它也會捕捉其他錯誤類型。

您可以在文稿中有多個 trap 語句。 每個錯誤類型只能由一個 trap 語句截獲。 發生終止錯誤時,PowerShell 會搜尋 trap 具有最特定相符專案的 ,從目前的執行範圍開始。

下列文稿範例包含錯誤。 腳本包含一般trap語句,可攔截任何終止錯誤,以及指定 CommandNotFoundException 類型的特定trap語句。

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

執行此文稿會產生下列結果:

Command error trapped
nonsenseString:
Line |
   5 |  nonsenseString
     |  ~~~~~~~~~~~~~~
     | 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 try again.

由於 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
RuntimeException:
Line |
   4 |  1/$null
     |  ~~~~~~~
     | Attempted to divide by zero.

嘗試除以零並不會建立 CommandNotFoundException 錯誤。 相反地,該錯誤會由另一 trap 個 語句截獲,而該語句會攔截任何終止錯誤。

截獲錯誤和範圍

如果終止錯誤發生在與 語句相同的範圍內 trap ,PowerShell 會執行 所 trap定義的語句清單。 執行會在錯誤之後繼續在語句中執行。 trap如果語句位於與錯誤不同的範圍內,則執行會繼續在下一個與語句位於相同範圍的語句中trap

例如,如果在函式中發生錯誤,而 trap 語句位於 函式中,腳本會繼續在下一個語句中。 下列文稿包含錯誤和 trap 語句:

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

function1

執行此文稿會產生下列結果:

An error:
NonsenseString:
Line |
   3 |      NonsenseString
     |      ~~~~~~~~~~~~~~
     | 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 try again.
function1 was completed

trap 式中的語句會捕捉錯誤。 顯示訊息之後,PowerShell 會繼續執行函式。 請注意, Function1 已完成。

將此與下列範例進行比較,其具有相同的錯誤和 trap 語句。 在此範例中 trap ,語句會在函式外部發生:

function function2 {
    NonsenseString
    "function2 was completed"
}

trap { "An error: " }

function2

執行函 Function2 式會產生下列結果:

An error:
NonsenseString:
Line |
   2 |      NonsenseString
     |      ~~~~~~~~~~~~~~
     | 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 try again.

在此範例中,「function2 已完成」命令未執行。 在這兩個範例中,終止錯誤會在 函式內發生。 不過,在此範例中 trap ,語句位於 函式之外。 在語句執行之後 trap ,PowerShell 不會回到 函式。

警告

針對相同的錯誤條件定義多個陷阱時,會使用範圍中第一個 trap 定義的語彙 (最高) 。

在下列範例中,只會 trap 執行 「whoops 1」 的 。

Remove-Item -ErrorAction Stop ThisFileDoesNotExist
trap { "whoops 1"; continue }
trap { "whoops 2"; continue }

重要

Trap 語句的範圍是其編譯位置。 如果您在函式或點來源腳本內有 trap 語句,當函式或點來源腳本結束時,就會移除內的所有 trap 語句。

使用 break 和 continue 關鍵詞

您可以使用 break 語句中的 trapcontinue 關鍵詞,判斷腳本或命令是否會在終止錯誤之後繼續執行。

如果您在語句清單中包含 break 語句 trap ,PowerShell 會停止函式或腳本。 下列範例函式會在 break 語句中使用 trap 關鍵詞:

function break_example {
    trap {
        "Error trapped"
        break
    }
    1/$null
    "Function completed."
}

break_example
Error trapped
ParentContainsErrorRecordException:
Line |
   6 |      1/$null
     |      ~~~~~~~
     | Attempted to divide by zero.

trap因為語句包含 break 關鍵詞,所以函式不會繼續執行,而且不會執行 “Function completed” 行。

如果您在 continue 語句中包含 trap 關鍵詞,則PowerShell會在造成錯誤的語句之後繼續,就像沒有 breakcontinue一樣。 continue不過,使用 關鍵詞時,PowerShell 不會將錯誤寫入錯誤數據流。

下列範例函式會在 continue 語句中使用 trap 關鍵詞:

function continue_example {
    trap {
        "Error trapped"
        continue
    }
    1/$null
    "Function completed."
}

continue_example
Error trapped
Function completed.

在截獲錯誤之後,函式會繼續執行,而「函式已完成」語句會執行。 錯誤不會寫入錯誤數據流。

備註

trap 語句提供簡單的方式來廣泛確保處理範圍內的所有終止錯誤。 如需更精細的錯誤處理,請使用 try/catch 使用 catch 語句定義陷阱的區塊。 語句 catch 只適用於相關聯 try 語句內的程序代碼。 如需詳細資訊,請參閱 about_Try_Catch_Finally

另請參閱

about_Break

about_Continue

about_Scopes

about_Throw

about_Try_Catch_Finally