簡短描述
描述處理終止錯誤的關鍵詞。
完整描述
終止錯誤會停止語句執行。 如果 PowerShell 未以某種方式處理終止錯誤,PowerShell 還會停止在當前管道中運行函數或腳本。 在其他語言中,例如 C#,終止錯誤稱為例外狀況。
Trap 關鍵詞會指定要在發生終止錯誤時執行的語句清單。 Trap 語句處理終止錯誤,並允許腳本或函數的執行繼續而不是停止。
Trap 語句也可能更複雜。 trap 的語句清單可以包含多個條件或函數調用。 陷阱可以寫入日誌、測試條件,甚至運行其他程式。
語法
Trap 語句的語法如下:
trap [[<error type>]] {<statement list>}
Trap 語句包括在發生終止錯誤時要運行的語句清單。 Trap 語句由關鍵字(可選)後跟類型表達式和語句塊組成 trap ,該語句塊包含在捕獲錯誤時要運行的語句清單。 類型表達式優化了陷阱捕獲的錯誤類型。
一個腳本或命令可以有多個 Trap 語句。 Trap 語句可以出現在腳本或命令中的任何位置。
捕捉所有終止錯誤
當發生未在腳本或命令中以其他方式處理的終止錯誤時,PowerShell 會檢查處理該錯誤的 Trap 語句。 如果存在 Trap 語句,PowerShell 將繼續運行 Trap 語句中的腳本或命令。
以下示例是一個非常簡單的 Trap 語句:
trap {"Error found."}
此 Trap 語句捕獲任何終止錯誤。
在下列範例中,函式包含導致運行時間錯誤的無稽之談字元串。
function TrapTest {
trap {"Error found."}
nonsenseString
}
TrapTest
執行此函數會傳回以下內容:
Error found.
以下範例包括一個 Trap 語句,該語句使用 $_ automatic 變數顯示錯誤:
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 then try
again.
這很重要
陷阱語句可以在給定範圍內的任意位置定義,但始終適用於該範圍內的所有語句。 在運行時,塊中的陷阱是在執行任何其他語句之前定義的。 在 JavaScript 中,這稱為 吊。 這意味著陷阱適用於該塊中的所有語句,即使執行尚未超過定義它們的點。 例如,在腳本末尾定義一個陷阱並在第一個語句中引發錯誤仍會觸發該陷阱。
攔截特定錯誤
一個腳本或命令可以有多個 Trap 語句。 可以定義陷阱來處理特定錯誤。
以下示例是捕獲特定錯誤 CommandNotFoundException 的 Trap 語句:
trap [System.Management.Automation.CommandNotFoundException]
{"Command error trapped"}
當函數或腳本遇到與已知命令不匹配的字串時,此 Trap 語句將顯示 「Command error trapped」 字串。 執行 Trap 語句列表之後,PowerShell 會將錯誤物件寫入錯誤數據流,然後繼續腳本。
PowerShell 使用 Microsoft .NET Framework 異常類型。 下列範例會指定 System.Exception 錯誤類型:
trap [System.Exception] {"An error trapped"}
CommandNotFoundException 錯誤類型繼承自 System.Exception 類型。 這個語句捕獲由未知命令創建的錯誤。 它也會捕捉其他錯誤類型。
腳本中可以有多個 Trap 語句。 每種錯誤類型只能由一個 Trap 語句捕獲。 發生終止錯誤時,PowerShell 會從當前執行範圍開始搜索具有最具體匹配項的陷阱。
下列文稿範例包含錯誤。 該腳本包括一個常規 Trap 語句,用於捕獲任何終止錯誤,以及一個指定 CommandNotFoundException 類型的特定Trap語句。
trap {"Other terminating error trapped" }
trap [System.Management.Automation.CommandNotFoundException] {
"Command error trapped"
}
nonsenseString
執行此文稿會產生下列結果:
Command error trapped
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.
At C:\temp\test\traptest.ps1:5 char:1
+ nonsenseString
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (nonsenseString:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
由於 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:\temp\test\traptest.ps1:5 char:1
+ 1/$null
+ ~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
嘗試除以零不會產生 CommandNotFoundException 錯誤。 相反,該錯誤由另一個 Trap 語句捕獲,該語句捕獲任何終止錯誤。
截獲錯誤和範圍
如果終止錯誤發生在與 Trap 語句相同的範圍內,PowerShell 將運行陷阱定義的語句清單。 發生錯誤後,程式碼會接著執行下一行。 如果 Trap 語句與錯誤位於不同的範圍內,則在與 Trap 語句位於同一範圍內的下一個語句繼續執行。
例如,如果函數中發生錯誤,並且 Trap 語句位於函數中,則腳本將在下一個語句中繼續。 以下文稿包含一個 error 和一個 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 語句捕獲錯誤。 顯示訊息之後,PowerShell 會繼續執行 函式。 請注意, Function1 已完成。
將此與以下示例進行比較,該示例具有相同的 error 和 Trap statement。 在此範例中,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 was completed」 命令。 在這兩個範例中,終止錯誤會在 函式內發生。 但是,在此示例中,Trap 語句位於函數外部。 PowerShell 在 Trap 語句運行後不會返回到函數。
謹慎
當為同一錯誤條件定義多個陷阱時,將使用按詞法定義的第一個陷阱(範圍內的最高)。
在以下示例中,僅運行具有 「whoops 1」 的 trap。
Remove-Item -ErrorAction Stop ThisFileDoesNotExist
trap { "whoops 1"; continue }
trap { "whoops 2"; continue }
break使用 and continue 關鍵字
您可以在 Trap 語句中使用 Break and Continue 關鍵字來確定文稿或命令在終止錯誤後是否繼續運行。
如果您在 Trap statement 清單中包含語句 Break ,PowerShell 將停止該函數或腳本。 以下範例函數在 Trap 語句中使用 Break 關鍵字:
function break_example {
trap {
"Error trapped"
break
}
1/$null
"Function completed."
}
break_example
Error trapped
Attempted to divide by zero.
At line:4 char:7
由於 Trap 語句包含關鍵字 Break ,因此函數不會繼續運行,並且不會運行 「Function completed」 行。
如果在 Trap 語句中包含語句 Continue ,PowerShell 將在導致錯誤的語句之後恢復,就像沒有 Break 或 Continue一樣。 但是, Continue 使用關鍵字時,PowerShell 不會將錯誤寫入錯誤流。
下列範例函式會在 Continue 語句中使用 Trap 關鍵詞:
function continue_example {
trap {
"Error trapped"
continue
}
1/$null
"Function completed."
}
continue_example
Error trapped
Function completed.
捕獲錯誤後,函數將恢復,並運行 「Function completed」 語句。 不會將錯誤寫入錯誤流。
註釋
trap 語句提供了一種簡單的方法,可以廣泛地確保處理作用域內的所有終止錯誤。 如需更精細的錯誤處理,請使用使用 try 語句定義陷阱的 /catchCatch 區塊。
Catch 語句僅適用於相關聯 Try 語句內的程序代碼。 如需詳細資訊,請參閱 about_Try_Catch_Finally。