Powershell捕获异常类型 [英] Powershell catching exception type
问题描述
是否有方便的方法来捕获异常和内部异常以进行尝试捕获?
Is there a convenient way to catch types of exceptions and inner exceptions for try-catch purposes?
示例代码:
$a = 5
$b = Read-Host "Enter number"
$c = $a / $b #error if $b -eq 0
$d = get-content C:\I\Do\Not\Exist
第3行将生成带有内部异常的运行时错误(已修复此命令$ Error [1] .Exception.InnerException.GetType()),第4行将生成标准"(?)类型异常($ Error [0] .Exception.GetType()).
Row #3 will generate a runtime error with an inner exception ( fixed this command $Error[1].Exception.InnerException.GetType()), row #4 will generate a "standard"(?) type of exception ($Error[0].Exception.GetType()).
是否可以通过同一行代码从这两者中获得所需的结果?
Is it possible to get the desired result from both of these with the same line of code?
Ad1:第3行出现错误
Ad1: error from row 3
At -path-:3 char:1
+ $c = $a / $b #error if $b -eq 0
+ ~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
Ad2:第4行出现错误
Ad2: error from row 4
get-content : Cannot find path 'C:\I\Do\Not\Exist' because it does not exist.
At -path-:4 char:6
+ $d = get-content C:\I\Do\Not\Exist
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\I\Do\Not\Exist:String)
[Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
为了清楚起见,我希望结果以某种方式返回DivideByZeroException和ItemNotFoundException
To make it clear, I want the result to return DivideByZeroException and ItemNotFoundException in some way
推荐答案
首先,您可以显式捕获特定的异常类型:
First of all, you can explicitly catch specific exception types:
$ErrorActionPreference = "Stop"
try {
1/0
}
catch [System.DivideByZeroException] {
$_.Exception.GetType().Name
}
try {
gi "c:\x"
}
catch [System.Management.Automation.ItemNotFoundException] {
$_.Exception.GetType().Name
}
DivideByZeroException
基本上只是 RuntimeException
的InnerException,从理论上讲,InnerExceptions可以无休止地嵌套:
The DivideByZeroException
is basically just the InnerException of RuntimeException
, and theoretically, the InnerExceptions could be endlessly nested:
catch {
$exception = $_.Exception
do {
$exception.GetType().Name
$exception = $exception.InnerException
} while ($exception)
}
但,您可以作为特殊情况处理 RuntimeException
.甚至PowerShell也会这样做.看第一个代码示例.即使指定了 inner 异常的类型,也会达到catch块.
BUT you can handle RuntimeException
as a special case. Even PowerShell does so. Look at the first code example. The catch-block is reached even though the type of the inner exception is specified.
您可以自己做类似的事情:
You could do something similar yourself:
catch {
$exception = $_.Exception
if ($exception -is [System.Management.Automation.RuntimeException] -and $exception.InnerException) {
$exception = $exception.InnerException
}
$exception.GetType().Name
}
注意,如果要捕获两个异常,则每个命令需要一个try-catch.否则,如果第一个失败,则第二个将不会执行.另外,您还必须为"Stop"
指定 $ ErrorActionPreference
来捕获非终止异常.
NOTE that you need one try-catch per command, if you want to catch both exceptions. Else the 2nd will not be executed if the 1st one fails. Also you have to specify $ErrorActionPreference
to "Stop"
to catch also non-terminating exceptions.
这篇关于Powershell捕获异常类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!