3

私の質問は次のとおりです。

  • このエラーがコードにスローされないのはなぜですか?
  • エラーをコードにスローするにはどうすればよいですか?
  • どこで/どうすれば自分でこれを理解できたでしょうか? / この「機能」のドキュメントはどこにありますか?

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

2 に答える 2

5

PowerShell チームから言われたことは、プロパティは書式設定と出力中に頻繁に使用されるため、プロパティからの例外は常にマスクされるということです。ScriptMethod の例外はマスクされません。これはデバッグの観点からは残念なことですが、一般的に、プロパティは状態の取得と設定以上のことを行うべきではありません。また、.NET 開発者は、プロパティがスローされることを期待していないことも付け加えておきます。

この動作が気に入らない場合は、http://connect.microsoft.comサイトでお気軽に問題を送信してください。PowerShell 開発者の少なくとも 1 人が、例外が常に非表示であること、つまり書式設定操作の外部であることを好まないことを知っています。これについて顧客の意見を聞くことは、開発者が動作を変更することを主張するのに役立ちます-少なくとも厳格なモードの場合.

于 2013-11-04T21:42:59.117 に答える
2

これは、ある種のスコープの問題かバグのどちらかだと本当に思います。割と簡単に再現できました。この問題を解決できた唯一の方法は、次のような回避策でした。

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

うまくいかないことに驚きました。

于 2013-11-04T03:31:48.413 に答える