about_Trap
簡短描述
描述處理終止錯誤的關鍵詞。
詳細描述
終止錯誤會停止語句執行。 如果 PowerShell 未以某種方式處理終止錯誤,PowerShell 也會停止在目前的管線中執行函式或腳本。 在其他語言中,例如 C#,終止錯誤稱為例外狀況。
trap
關鍵詞會指定要在發生終止錯誤時執行的語句清單。
trap
語句可以透過下列方式處理終止錯誤:
處理
trap
語句區塊后顯示錯誤,並繼續執行包含 的trap
腳本或函式。 這是預設行為。注意
在次級腳本區塊中發生終止錯誤時,例如
if
語句或foreach
迴圈,區塊中的trap
語句會執行,並在次級腳本區塊外的下一個語句繼續執行。在語句中
trap
顯示包含break
usingtrap
的腳本或函式錯誤和執行。讓錯誤保持沉默,但在語句中使用 ,繼續執行包含
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 : 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 line:3 char:5
+ nonsenseString
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (nonsenseString:String) []
, CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
下列範例包含使用 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 : 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 line:3 char:5
+ nonsenseString
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (nonsenseString:String) []
, CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
重要
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 類型。 此語句會攔截未知命令引發的任何錯誤。 它也會捕捉其他錯誤類型。
您可以藉由檢查錯誤物件來尋找錯誤的例外狀況類型。 下列範例示範如何取得工作階段中最後一個錯誤之例外狀況的完整名稱:
nonsenseString
$Error[0].Exception.GetType().FullName
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 line:1 char:1
+ nonsenseString
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (nonsenseString:String) []
, CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
System.Management.Automation.CommandNotFoundException
您可以在文稿中有多個 trap
語句。 只有一個 trap
語句可以設陷每個錯誤類型。 當發生終止錯誤時,PowerShell 會搜尋 trap
具有最特定相符專案的 ,從目前的腳本區塊開始執行。
下列文稿範例包含錯誤。 腳本包含一般trap
語句,可攔截任何終止錯誤,以及指定 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 line: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 line:5 char:1
+ 1/$null}
+ ~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
嘗試除以零並不會建立 CommandNotFoundException 錯誤。 另一個 trap
語句會捕捉任何終止錯誤,它會將除以零錯誤來設陷。
在腳本區塊中截獲錯誤
根據預設,擲回終止錯誤時,執行會傳送至 trap 語句。 執行 trap
區塊之後,控件會回到錯誤位置之後的下一個語句區塊。
例如,當語句中 foreach
發生終止錯誤時, trap
語句會在 區塊之後 foreach
的下一個語句繼續執行,而不是在 區塊內 foreach
執行。
trap { 'An error occurred!'}
foreach ($x in 3..-1) {
"1/$x = "
"`t$(1/$x)"
}
'after loop'
1/3 =
0.333333333333333
1/2 =
0.5
1/1 =
1
1/0 =
An error occurred!
Attempted to divide by zero.
At line:3 char:4
+ 1/$x
+ ~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
after loop
在輸出中,您可以看到迴圈會繼續直到最後一個反覆項目為止。 當腳本嘗試將 1 除以 0 時,PowerShell 會擲回終止錯誤。 腳本會略過腳本區塊的 foreach
其餘部分、執行 try
語句,並在腳本區塊之後 foreach
繼續。
截獲錯誤和範圍
如果終止錯誤發生在與 語句相同的腳本區塊 trap
中,PowerShell 會執行 所 trap
定義的語句清單。 錯誤之後,語句會繼續執行。
trap
如果語句位於與錯誤不同的腳本區塊中,則執行會在與 語句位於相同腳本區塊trap
中的下一個語句繼續執行。
例如,如果函式中發生錯誤,而 trap
語句位於 函式中,腳本會繼續在下一個語句中。 下列文稿包含錯誤和 trap
語句:
function function1 {
trap { 'An error: ' }
NonsenseString
'function1 was completed'
}
function1
執行此文稿會產生下列結果:
An error:
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 line:3 char:5
+ NonsenseString
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (NonsenseString:String) []
, CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
function1 was completed
函 trap
式中的語句會捕捉錯誤。 顯示訊息之後,PowerShell 會繼續執行 函式。 請注意, Function1
在語句之後 trap
完成。
比較此行為與下列範例,其錯誤和 trap
語句相同。 在此範例中 trap
,語句會在函式外部發生:
function function2 {
NonsenseString
'function2 was completed'
}
trap { 'An error:' }
function2
執行函 Function2
式會產生下列結果:
An error:
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 line:2 char:5
+ NonsenseString
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (NonsenseString:String) []
, CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
在此範例中 function2 was completed
,命令並未執行。 在這兩個範例中,終止錯誤會在 函式內發生。 不過,在此範例中 trap
,語句位於 函式之外。 PowerShell 不會在語句執行之後 trap
返回函式。
警告
針對相同的錯誤條件定義多個陷阱時,會使用第一個 trap
定義語彙(腳本區塊中最高的) 語彙。
在下列範例中,只有 trap
執行的 whoops 1
。
Remove-Item -ErrorAction Stop ThisFileDoesNotExist
trap { 'whoops 1'; continue }
trap { 'whoops 2'; continue }
重要
trap
語句的範圍是其編譯位置。 如果您在函式或點來源腳本內有 trap
語句,當函式或點來源腳本結束時,就會移除內部的所有 trap
語句。
使用中斷並繼續關鍵詞
您可以使用 break
語句中的 continue
和 trap
關鍵詞來判斷文稿或命令在終止錯誤之後是否繼續執行。
如果您在語句清單中包含 break
語句 trap
,PowerShell 會停止函式或腳本。 下列範例函式會在 break
語句中使用 trap
關鍵詞:
function break_example {
trap {
'Error trapped'
break
}
1/$null
'Function completed.'
}
break_example
Error trapped
Attempted to divide by zero.
At line:6 char:5
+ 1/$null
+ ~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorR
ecordException
+ FullyQualifiedErrorId : RuntimeException
trap
因為語句包含 break
關鍵詞,因此函式不會繼續執行,而且Function completed
行不會執行。
如果您在 continue
語句中包含 trap
關鍵詞,PowerShell 會在造成錯誤的 語句之後繼續,就像沒有 break
或 continue
一樣。
continue
不過,使用 關鍵詞時,PowerShell 不會將錯誤寫入錯誤數據流。
下列範例函式會在 continue
語句中使用 trap
關鍵詞:
function ContinueExample {
trap {
'Error trapped'
continue
}
foreach ($x in 3..-1) {
"1/$x = "
"`t$(1/$x)"
}
'End of function'
}
ContinueExample
1/3 =
0.333333333333333
1/2 =
0.5
1/1 =
1
1/0 =
Error trapped
End of function
函式會在截獲錯誤之後繼續執行,而語句會 End of function
執行。 不會將錯誤寫入錯誤數據流。
備註
trap
語句提供一種方法,以確保處理腳本區塊內的所有終止錯誤。 如需更精細的錯誤處理,請使用 try
/catch
使用 catch
語句定義陷阱的區塊。 語句 catch
只適用於相關聯 try
語句內的程序代碼。 如需詳細資訊,請參閱 about_Try_Catch_Finally。