Jli*_*isk 9 powershell exception
有没有一种方便的方法来捕获异常类型和内部异常以用于 try-catch 目的?
示例代码:
$a = 5
$b = Read-Host "Enter number"
$c = $a / $b #error if $b -eq 0
$d = get-content C:\I\Do\Not\Exist
Run Code Online (Sandbox Code Playgroud)
第 3 行将生成带有内部异常的运行时错误(编辑:修复了此命令 $Error[1].Exception.InnerException.GetType()),第 4 行将生成“标准”(?) 类型的异常 ($错误[0].Exception.GetType())。
是否可以使用同一行代码从这两者中获得所需的结果?
Ad1:第 3 行错误
At -path-:3 char:1
+ $c = $a / $b #error if $b -eq 0
+ ~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
Run Code Online (Sandbox Code Playgroud)
Ad2:第 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
Run Code Online (Sandbox Code Playgroud)
编辑:为了说清楚,我希望结果以某种方式返回 DivideByZeroException 和 ItemNotFoundException
mar*_*sze 10
首先,您可以显式捕获特定的异常类型:
$ErrorActionPreference = "Stop"
try {
1 / 0
}
catch [System.DivideByZeroException] {
$_.Exception.GetType().Name
}
try {
Get-Item "c:\does-not-exist"
}
catch [System.Management.Automation.ItemNotFoundException] {
$_.Exception.GetType().Name
}
Run Code Online (Sandbox Code Playgroud)
基本上只是DivideByZeroException的 InnerException RuntimeException,理论上,InnerException 可以无限嵌套:
catch {
$exception = $_.Exception
do {
$exception.GetType().Name
$exception = $exception.InnerException
} while ($exception)
}
Run Code Online (Sandbox Code Playgroud)
但你可以RuntimeException作为特殊情况来处理。甚至 PowerShell 也这样做。看第一个代码示例。即使指定了内部异常的类型,也会到达 catch 块。
你可以自己做类似的事情:
catch {
$exception = $_.Exception
if ($exception -is [System.Management.Automation.RuntimeException] -and $exception.InnerException) {
$exception = $exception.InnerException
}
$exception.GetType().Name
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果您想捕获两个异常,则每个命令都需要一个 try-catch。否则,如果第一个失败,第二个将不会执行。此外,您还必须指定$ErrorActionPreferenceto"Stop"来捕获非终止异常。
| 归档时间: |
|
| 查看次数: |
16305 次 |
| 最近记录: |