Search code examples
exceptionpowershellextension-methodsswallowed-exceptions

powershell call to extended property fails silently ( Added via Update-TypeData )


My questions are:

  • Why is this error not being thrown out into the code?
  • How can I make the error get thrown up into the code?
  • Where / How could I have figured this out on my own? / Where is the documentation for this "feature"?

    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.]
    #>
    

Solution

  • What I've been told from the PowerShell team is that exceptions from properties are always masked as properties are heavily used during formatting & output. Exceptions are not masked for ScriptMethod. While this is unfortunate from a debugging stand-point, properties in general should not be doing much more than getting & setting state. I'd also add that .NET devs don't expect a property get to throw.

    If you don't like this behavior feel free to submit an issue on the http://connect.microsoft.com site. I know at least one of the PowerShell devs doesn't like that the exception is always hidden i.e. even outside of formatting operations. Having some customer input on this could help the dev make a case for changing the behavior - at least when in strict mode.