try
块抛出异常但在catch
块Resolve-Error
方法中获取$Error
为null(数组中为零元素),尽管$_.Exception
给出我正确的信息。可能是什么原因以及如何解决?
function Resolve-Error($ErrorRecord=$Error[0]) {
$ErrorRecord | Format-List * -Force
$ErrorRecord.InvocationInfo | Format-List *
$Exception = $ErrorRecord.Exception
for ($i = 0; $Exception; $i++, ($Exception = $Exception.InnerException)) {
"$i" * 80
$Exception | Format-List * -Force
}
}
try {
dsfhjbahdjfb
} catch {
Write-Host $_.Exception
if ($_.Exception.Response -ne $null) {
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd();
Write-Host $responseBody
}
Resolve-Error
throw "Exception....."
}