PowerShell对扩展属性的调用无提示失败(通过Update-TypeData添加)

时间:2013-11-03 23:43:28

标签: exception powershell extension-methods swallowed-exceptions

我的问题是:

  • 为什么这个错误没有被抛出代码?
  • 如何将错误抛到代码中?
  • 我/我怎么能自己解决这个问题? /这个“功能”的文档在哪里?

    Function demo_problem
    {
        describe_global_error_variable
        $ret_value = "be loud please".extended_property
        write-host "returned a value=[$ret_value]."
        describe_global_error_variable
    }
    
    
    Function describe_global_error_variable
    {
        $cnt = $Error.Count
        write-host "`$Error .Count=[$($cnt)]." 
        $i=0
        while ($i -lt $cnt) {
            write-host "`$Error[$i].Exception.Message=[$($Error[$i].Exception.Message)]"
            $i += 1
            }
    }
    
    $script_block_throws = { 
            write-host "at beginning of script_block for script_block_throws.  `$this=[$this]."
            1/0
            return $true
            write-host "at end of script_block for script_block_throws.  `$this=[$this]."
        }
    
    $script_block_try_catch_throw = { 
            write-host "at beginning of script_block for script_block_try_catch_throw.  `$this=[$this]."
            try
            {
                1/0
                return $true
            }
            catch [Exception]{
                write-host "script_block_try_catch_throw caught an exception"
                throw "caught an exception" 
            }
            return $false
            write-host "at end of script_block for script_block_try_catch_throw.  `$this=[$this]."
        }
    
    try {
        Update-TypeData -Value:$script_block_throws -TypeName:System.String -MemberName:extended_property -Force -MemberType:ScriptProperty 
        demo_problem
        Update-TypeData -Value:$script_block_try_catch_throw -TypeName:System.String -MemberName:extended_property -Force -MemberType:ScriptProperty 
        demo_problem
    }
    catch [Exception]{
        write-host "exception got thrown out of the script block...."   
    }
    
    <#
    PS C:\ .\powershell_call_to_extended_property_fails_silently.ps1
    $Error .Count=[0]. \
    at beginning of script_block for script_block_throws.  $this=[be loud please].
    returned a value=[].
    $Error .Count=[1]. \
    $Error[0].Exception.Message=[Attempted to divide by zero.]
    $Error .Count=[1]. \
    $Error[0].Exception.Message=[Attempted to divide by zero.]
    at beginning of script_block for script_block_try_catch_throw.  $this=[be loud please].
    script_block_try_catch_throw caught an exception
    returned a value=[].
    $Error .Count=[3]. \
    $Error[0].Exception.Message=[caught an exception]
    $Error[1].Exception.Message=[Attempted to divide by zero.]
    $Error[2].Exception.Message=[Attempted to divide by zero.]
    #>
    

2 个答案:

答案 0 :(得分:5)

PowerShell团队告诉我的是,在格式化过程中大量使用属性时,属性的异常总是被掩盖。输出。 ScriptMethod不会屏蔽异常。虽然从调试的角度来看这是不幸的,但一般来说,属性 不仅仅是。设置状态。我还要补充一点,.NET开发人员不希望属性被抛出。

如果您不喜欢此行为,请随时在http://connect.microsoft.com网站上提交问题。我知道至少有一个PowerShell开发人员不喜欢总是隐藏的异常,即使在格式化操作之外。对此进行一些客户输入可以帮助开发人员改变行为 - 至少在严格模式下。

答案 1 :(得分:1)

我真的认为这是某种范围问题或错误。我很容易重现它。我能够解决问题的唯一方法是通过一种解决方法,看起来像这样:

Update-TypeData -Value:$script_block_try_catch_throw -TypeName:System.String -MemberName:extended_property -Force -MemberType:ScriptProperty -ErrorAction Stop
demo_problem
if($error[0].Exception.Message -eq "caught an exception")
{
    throw "I only exist to trigger the catch block"
}

我甚至试过这个都无济于事:

Update-TypeData -Value:{try{&$script_block_try_catch_throw}catch{throw "Error!"}} -TypeName:System.String -MemberName:extended_property -Force -MemberType:ScriptProperty -ErrorAction Stop

我感到惊讶的是没有用。