PowerShell中的异常和错误

时间:2017-09-13 21:56:20

标签: powershell error-handling exception-handling

try块抛出异常但在catchResolve-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....."
}

0 个答案:

没有答案