应用到: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0
主题
about_Trap
简短说明
介绍处理终止错误的关键字。
详细说明
终止错误使语句停止运行。如果 Windows PowerShell® 不以某种方式处理终止错误,则 Windows PowerShell 还会停止运行当前管道中的函数或脚本。在其他语言(例如 C#)中,终止错误称为异常。
Trap 关键字指定要在发生终止错误时运行的语句列表。Trap 语句处理终止错误,并且允许继续执行脚本或函数,而不停止。
语法
Trap 语句具有以下语法:
trap [[<error type>]] {<statement list>}
Trap 语句包含要在发生终止错误时运行的语句列表。Trap 关键字可以选择指定错误类型。错误类型需要括号。
脚本或命令可以具有多个 Trap 语句。Trap 语句可以显示在脚本或命令中的任何位置。
捕获所有终止错误
当发生终止错误且未在脚本或命令中以另一种方式进行处理时,Windows PowerShell 将检查处理该错误的 Trap 语句。如果存在 Trap 语句,则 Windows PowerShell 在 Trap 语句中继续运行脚本或命令。
以下示例是非常简单的 Trap 语句:
trap {"Error found."}
此 Trap 语句捕获任何终止错误。以下示例是包含此 Trap 语句的函数:
function TrapTest {
trap {"Error found."}
nonsenseString
}
此函数包含导致错误的无意义字符串。运行此函数将返回以下内容:
C:\PS> TrapTest
Error found.
以下示例包含通过使用 $_ 自动变量显示错误的 Trap 语句:
function TrapTest {
trap {"Error found: $_"}
nonsenseString
}
运行此版本的函数将返回以下内容:
C:\PS> 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.
Trap 语句还可以更复杂。Trap 语句可以包含多个条件或函数调用。它可以记录、测试甚至是运行其他程序。
捕获指定的终止错误
以下示例是捕获 CommandNotFoundException 错误类型的 Trap 语句:
trap [System.Management.Automation.CommandNotFoundException]
{"Command error trapped"}
当函数或脚本遇到与已知命令不匹配的字符串时,此 Trap 语句将显示“命令错误已捕获”字符串。在 Trap 语句列表中运行任何语句后,Windows PowerShell 将错误对象写入错误流,然后继续执行脚本。
Windows PowerShell 使用 Microsoft .NET Framework 异常类型。以下示例指定 System.Exception 错误类型:
trap [System.Exception] {"An error trapped"}
CommandNotFoundException 错误类型从 System.Exception 类型继承。此语句捕获由未知命令创建的错误。它还会捕获其他错误类型。
一个脚本中可以具有多个 Trap 语句。每个错误只能由一个 Trap 语句捕获。如果出现错误,并且有多个 Trap 语句可用,则 Windows PowerShell 将使用带有与该错误匹配的最具体的错误类型的 Trap 语句。
以下脚本示例包含错误。该脚本包含捕获任何终止错误的通用 Trap 语句和指定 CommandNotFoundException 类型的特定 Trap 语句。
trap {"Other terminating error trapped" }
trap [System.Management.Automation.CommandNotFoundException] {"Command error trapped"}
nonsenseString
运行此脚本将生成以下结果:
Command error trapped
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 <<<<
由于 Windows 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:PS> errorX.ps1:3 char:7
+ 1/ <<<< $null
尝试以零作除数不会创建 CommandNotFoundException 错误。相反,该错误由可捕获任何终止错误的其他 Trap 语句捕获。
捕获错误和范围
如果终止错误发生的范围与 Trap 语句相同,则在运行 Trap 语句后,Windows PowerShell 将从错误之后的语句继续运行。如果 Trap 语句所在的范围与错误不同,则执行将从与该 Trap 语句相同的范围内的下一个语句继续运行。
例如,如果错误发生在函数中,并且 Trap 语句在该函数中,则脚本将从下一个语句继续运行。例如,以下脚本包含一个错误和一个 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 语句可捕获错误。在显示消息后,Windows PowerShell 将继续运行函数。请注意,Function1 已完成。
将此函数与以下示例比较,该示例具有相同的错误和 Trap 语句。在此示例中,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 已完成”命令。尽管两个终止错误都发生在函数内,但如果 Trap 语句在函数外,则 Windows PowerShell 不会在 Trap 语句运行后返回到函数中。
使用 Break 和 Continue 关键字
你可以在 Trap 语句中使用 Break 和 Continue 关键字,以确定脚本或命令是否在终止错误后继续运行。
如果 Break 语句包含在 Trap 语句列表中,则 Windows PowerShell 将停止函数或脚本。以下示例函数在 Trap 语句中使用 Break 关键字:
C:\PS> function break_example {
trap {"Error trapped"; break;}
1/$null
"Function completed."
}
C:\PS> break_example
Error trapped
Attempted to divide by zero.
At line:4 char:7
由于 Trap 语句包含了 Break 关键字,函数不会继续运行,并且未运行“函数已完成”行。
如果 Continue 语句包含在 Trap 语句中,Windows PowerShell 将在导致错误的语句后继续运行,就像在没有 Break 或 Continue 的情况下一样。但是在有 Continue 关键字的情况下,Windows PowerShell 不会将错误写入错误流。
以下示例函数在 Trap 语句中使用 Continue 关键字:
C:\PS> function continue_example {
trap {"Error trapped"; continue;}
1/$null
"Function completed."}
C:\PS> continue_example
Error trapped
Function completed.
函数在捕获错误后继续运行,并且将运行“函数已完成”语句。不会将任何错误写入错误流。
另请参阅
about_Break
about_Continue
about_Scopes
about_Throw
about_Try_Catch_Finally