共用方式為


about_Trap

簡短描述

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

完整描述

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

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

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

    注意

    當終止錯誤發生在次級腳本區塊中,例如 if 語句或 foreach 迴圈時,區塊中的 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 a name of a cmdlet,
function, script file, or executable 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 a name of a cmdlet,
function, script file, or executable 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 類型。 此語句會攔截未知命令所引發的任何錯誤。 它也會捕捉其他錯誤類型。

您可以藉由檢查錯誤物件來尋找錯誤的例外狀況類型。 下列範例示範如何取得工作階段中最後一個錯誤之例外狀況的完整名稱:

nonsenseString
$Error[0].Exception.GetType().FullName
nonsenseString: The term 'nonsenseString' is not recognized as a name of a
cmdlet, function, script file, or executable program. Check the spelling
of the name, or if a path was included, verify that the path is correct
and try again.

System.Management.Automation.CommandNotFoundException

您可以在文稿中有多個 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 a name of a cmdlet,
function, script file, or executable 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 |
   5 |  1/$null
     |  ~~~~~~~
     | Attempted to divide by zero.

嘗試除以零不會建立 CommandNotFoundException 錯誤。 另一 trap 個語句會捕捉任何終止的錯誤,會捕捉除以零錯誤。

截獲腳本區塊中的錯誤

根據預設,當擲回終止錯誤時,執行會傳送至陷阱語句。 執行 trap 區塊之後,控件會回到錯誤位置之後的下一個語句區塊。

例如,當語句中發生終止錯誤時,trap語句會在 區塊之後foreach的下一個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:
Line |
   3 |     1/$x
     |     ~~~~
     | Attempted to divide by zero.
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:
Line |
   3 |      NonsenseString
     |      ~~~~~~~~~~~~~~
     | The term 'NonsenseString' is not recognized as a name of a cmdlet,
function, script file, or executable 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 語句。 在此範例中 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 a name of a cmdlet,
function, script file, or executable 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 ,語句位於 函式之外。 在語句執行之後 trap ,PowerShell 不會回到 函式。

警告

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

在下列範例中,只有 執行 trapwhoops 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.

函式會在截獲錯誤之後繼續執行,而語句會 Function completed 執行。 錯誤不會寫入錯誤數據流。

備註

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

另請參閱