.net Powershell:在Try/Catch块中使用聚合异常的句柄方法

.net Powershell:在Try/Catch块中使用聚合异常的句柄方法,.net,multithreading,powershell,powershell-4.0,.net,Multithreading,Powershell,Powershell 4.0,我有一个任务,对大约50-60个可能的设备执行反向DNS查找。在任何情况下都不会引发聚合异常 如果只查看控制台输出(没有红色文本),我的方法似乎有效,但是$Error变量中仍然可以找到异常 到目前为止,我还没有找到在PowerShell中使用Handle方法的示例 我将不再使用从以下链接获得的示例,但我对C#或VB不是很熟悉。我可能在翻译到PowerShell时遇到问题 我想知道的是。。。我哪里出错了,或者异常实际得到了处理 下面的代码应该说明我陷入困境的原因。它将创建一个由3个异步反向DNS

我有一个任务,对大约50-60个可能的设备执行反向DNS查找。在任何情况下都不会引发聚合异常

如果只查看控制台输出(没有红色文本),我的方法似乎有效,但是$Error变量中仍然可以找到异常

到目前为止,我还没有找到在PowerShell中使用Handle方法的示例

我将不再使用从以下链接获得的示例,但我对C#或VB不是很熟悉。我可能在翻译到PowerShell时遇到问题

我想知道的是。。。我哪里出错了,或者异常实际得到了处理

下面的代码应该说明我陷入困境的原因。它将创建一个由3个异步反向DNS查找组成的任务,该任务将失败。调用WaitAll应该会产生预期的聚合异常,并且似乎已被处理,但仍在$Error中观察到

$Task = @('testComp1','testComp2','testComp3') | ForEach-Object {
    [pscustomobject]@{
        Computername = $_
        Task = [System.Net.DNS]::GetHostAddressesAsync($_)
    }
}

try
{
    [Void][Threading.Tasks.Task]::WaitAll($Task.Task)
}

catch [System.AggregateException]
{
    $_.Exception.Handle({
        param($ex)

        if ( $ex.GetType().Name -eq 'SocketException' ) {
            Write-Host 'Expected SocketException'
            return $true
        } else {
            return $false
        }
    })
}

$Error[0]

$Error
无关

Handle
方法的作用类似于对
aggregateeexception
中的异常进行筛选 谓词已返回false的异常将以新的
aggregateeexception
结束,该异常将被抛出

见上的备注

谓词的每次调用都返回true或false以指示 是否处理了异常。在所有调用之后(如果有) 异常未处理,所有未处理的异常将放入 将引发的新AggregateException

一个例子。

下面将抛出一个
aggregateeexception
,其中包含一个
DivideByZeroException
和一个
索引自动失效异常

请注意,第一个
catch
块显示了两个内部异常(#0和#1)。
只有
indexootfrangeexception
得到处理

这导致抛出一个新的
aggregateeexception
,但这次只包含
DivideByZeroException
。 请注意,第二个
catch
块仅显示1个内部异常

try
{
    try
    {
        $divideByZeroException = (New-Object -TypeName System.DivideByZeroException)
        $indexOutOfRangeException = (New-Object -TypeName System.IndexOutOfRangeException)
        throw (New-Object -TypeName System.AggregateException -ArgumentList $divideByZeroException,$indexOutOfRangeException)
    }
    catch [System.AggregateException]
    {        
        $_.Exception.ToString() | out-host

        # System.AggregateException: One or more errors occurred. ---> System.DivideByZeroException: Attempted to divide by zero.
        # --- End of inner exception stack trace ---
        # ---> (Inner Exception #0) System.DivideByZeroException: Attempted to divide by zero.<---
        # ---> (Inner Exception #1) System.IndexOutOfRangeException: Index was outside the bounds of the array.<---

        $_.Exception.Handle({
            param($ex)

            if ($ex.GetType().Name -eq 'IndexOutOfRangeException' ) {                
                return $true
            } else {             
                return $false
            }
        })
    }
}
catch [System.AggregateException]
{    
    $_.Exception.ToString() | out-host

    # System.AggregateException: One or more errors occurred. --->         System.DivideByZeroException: Attempted to divide by zero.
    # --- End of inner exception stack trace ---
    #  at System.AggregateException.Handle(Func`2 predicate)
    #   at CallSite.Target(Closure , CallSite , Object , ScriptBlock )
    #---> (Inner Exception #0) System.DivideByZeroException: Attempted to divide by zero.<---
}
试试看
{
尝试
{
$divideByZeroException=(新对象-TypeName System.divideByZeroException)
$indexOutOfRangeException=(新对象-TypeName System.indexOutOfRangeException)
抛出(新对象-TypeName System.AggregateException-ArgumentList$divideByZeroException,$indexOutOfRangeException)
}
catch[System.AggregateException]
{        
$\u0.Exception.ToString()|输出主机
#System.AggregateException:发生一个或多个错误。-->System.DivideByZeroException:试图除以零。
#---内部异常堆栈跟踪的结束---
#-->(内部异常#0)System.DivideByZeroException:尝试被零除。(内部异常#1)System.IndexAutoFrangException:索引超出数组的边界。System.DivideByZeroException:尝试被零除。
#---内部异常堆栈跟踪的结束---
#位于System.AggregateException.Handle(Func`2谓词)
#目标(闭包、调用站点、对象、脚本块)

#--->(内部异常#0)System.DivideByZeroException:尝试除以零。谢谢!我只是想确认一下,因为它仍然在$Error中。