簡短描述
描述處理終止錯誤的關鍵詞。
完整描述
終止錯誤會停止語句執行。 如果 PowerShell 未以某種方式處理終止錯誤,PowerShell 還會停止在當前管道中運行函數或腳本。 在其他語言中,例如 C#,終止錯誤稱為例外狀況。
trap 關鍵詞會指定要在發生終止錯誤時執行的語句清單。
trap 語句可以透過下列方式處理終止錯誤:
處理
trap語句區塊后顯示錯誤,並繼續執行包含trap的腳本或函式。 此為預設行為。備註
在次級腳本區塊中發生終止錯誤時,例如
if語句或foreach迴圈,就會執行trap區塊中的語句,並在次級腳本區塊以外的下一個語句繼續執行。使用
trap語句中的break顯示錯誤並中止執行包含trap的腳本或函式。藉由在
trap語句中使用continue,忽略錯誤,並繼續執行包含trap的腳本或功能。
trap 的語句清單可以包含多個條件或函數調用。
trap 可以寫入記錄、測試條件,甚至執行另一個程式。
語法
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 語句將顯示 「Command error trapped」 字串。
執行 trap 語句列表之後,PowerShell 會將錯誤物件寫入錯誤數據流,然後繼續腳本。
PowerShell 使用 .NET 例外狀況類型。 下列範例會指定 System.Exception 錯誤類型:
trap [System.Exception] {"An error trapped"}
CommandNotFoundException 錯誤類型繼承自 System.Exception 類型。 這個語句捕獲由未知命令創建的錯誤。 它也會捕捉其他錯誤類型。
您可以在腳本中使用多個 trap 語句。 每種錯誤類型只能由一個 trap 語句捕獲。 發生終止錯誤時,PowerShell 會從當前執行範圍開始搜索具有最具體匹配項的 。trap
下列文稿範例包含錯誤。 腳本包含一般 trap 語句,可攔截任何終止錯誤,以及指定 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 錯誤。 此終止錯誤由specific 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 錯誤。 相反,該錯誤由 other trap 語句捕獲,該語句捕獲任何終止錯誤。
在腳本區塊中捕捉錯誤
根據預設,擲回終止錯誤時,執行會傳送至 trap 語句。 執行 trap 區塊之後,控件會在錯誤的位置之後傳回下一個語句區塊。
例如,在 foreach 語句中發生終止錯誤時,trap 語句會在 foreach 區塊之後的下一個語句繼續執行,而不是在 foreach 區塊內執行。
trap { 'An error occurred!'}
foreach ($x in 3..0) {
1/$x
'after division'
}
'after loop'
0.333333333333333
after division
0.5
after division
1
after division
An error occurred!
RuntimeException: untitled:Untitled-1:3:4
Line |
3 | 1/$x
| ~~~~
| Attempted to divide by zero.
after loop
在上面的輸出中,你可以看到迴圈一直持續到最後一次反覆運算。
當腳本嘗試將 1 除以 0 時,將引發終止錯誤。 跳過 scriptblock 的其餘部分 foreach , try 運行語句,並且腳本在 scriptblock 之後 foreach 繼續。
截獲錯誤和範圍
如果在與 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 已完成。
將此與以下示例進行比較,該示例具有相同的 error 和 trap statement。 在此範例中,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 was completed」 命令。 在這兩個範例中,終止錯誤會在 函式內發生。 不過,在此範例中,trap 語句位於 函式之外。 PowerShell 在語句運行后 trap 不會返回到函數中。
謹慎
當為同一錯誤條件定義多個陷阱時,將使用第一個詞法定義的陷阱 trap (範圍內的最高)。
在以下範例中,僅 trap 執行with 「whoops 1」 的 。
Remove-Item -ErrorAction Stop ThisFileDoesNotExist
trap { "whoops 1"; continue }
trap { "whoops 2"; continue }
這很重要
Trap 語句的範圍限定為它的編譯位置。 如果您在函式或點來源腳本內有 trap 語句,當函式或點來源腳本結束時,就會移除內部的所有 trap 語句。
使用 break 和 continue 關鍵字
您可以使用 break 語句中的 continue 和 trap 關鍵詞來判斷文本或命令在終止錯誤之後是否繼續執行。
如果您在 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 會在造成錯誤的語句之後繼續執行,就像沒有 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。