2

我的問題是:PowerShell中調用擴展屬性失敗默默(通過更新-TypeData增補)

  • 爲什麼這個錯誤沒有被扔到代碼?
  • 我該如何讓錯誤被拋入代碼?
  • 哪裏/我怎麼能自己想出來的? /這個「功能」的文檔在哪裏?

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

回答

5

從PowerShell團隊得知,屬性的例外總是被屏蔽,因爲在格式化&輸出期間會頻繁使用屬性。 ScriptMethod不會屏蔽異常。雖然從調試的角度來看這是不幸的,但一般來說不是的性能要比獲得&的設置狀態要好得多。我還要補充一點,.NET開發人員不希望某個屬性出現問題。

如果您不喜歡這種行爲,請隨時在http://connect.microsoft.com網站上提交問題。我知道至少有一個PowerShell開發者不喜歡這種例外情況,即使在格式化操作之外,隱藏的也是總是。在這方面有一些客戶意見可以幫助開發者改變行爲 - 至少在嚴格模式下。

+0

謝謝你的信息。 – MaasSql

+0

@ keith-hill屬性**做**更多,看看WPF的實現狀態的獲取和設置只是它的一個核心,Powershell實際上與WPF非常相似,因爲它們的屬性對於它們的視覺表現來說也是不可替代的,所有例外都是靜音的,即使是像惰性評估這樣簡單和標準的場景對於很多任務來說可能是不可能的/不負責任的你確定至少沒有一個他們可以通過的異常類型嗎?可能沒有另一個.NET團隊會有球來靜音所有異常IL功能 – mtman

+0

我認爲這是最煩人的,因爲異常只能在運行時生成,因此,如果有人想在分配值之前想進行類型檢查,這將是p花邊去做。 – wesm

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 

我很驚訝,沒」工作。

+0

謝謝你的嘗試。當然,添加一個$ Error的檢查實際上是不可行的這些塊意味着是擴展方法。要求開發人員記住哪些方法需要檢查$ ERROR無疑會失敗。 – MaasSql

+0

+1因爲,如果基思山是正確的,它肯定聽起來像這是一個設計.... :( – MaasSql