欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PowerShell: Try...Catch...Finally 實(shí)現(xiàn)方法

 更新時(shí)間:2013年11月01日 00:43:50   作者:  
PowerShell 本身有很多很好的錯誤控制,但是習(xí)慣于.net編程的人員,更喜歡用Try Catch Finally方法,尤其當(dāng)有一段代碼必須被執(zhí)行到的時(shí)候?,F(xiàn)在好了,adweigert 想出了一個好方法來實(shí)現(xiàn)。這個函數(shù)已經(jīng)在多種情況下測試過,希望能對你有幫助

復(fù)制代碼 代碼如下:

function Try
    {
        param
        (
            [ScriptBlock]$Command = $(throw "The parameter -Command is required."),
            [ScriptBlock]$Catch   = { throw $_ },
            [ScriptBlock]$Finally = {}
        )

        & {
            $local:ErrorActionPreference = "SilentlyContinue"

            trap
            {
                trap
                {
                    & {
                        trap { throw $_ }
                        &$Finally
                    }

                    throw $_
                }

                $_ | & { &$Catch }
            }

            &$Command
        }

        & {
            trap { throw $_ }
            &$Finally
        }
    }

使用示例:

復(fù)制代碼 代碼如下:

# Example usage

    Try {
        echo " ::Do some work..."
        echo " ::Try divide by zero: $(0/0)"
    } -Catch {
        echo "  ::Cannot handle the error (will rethrow): $_"
        #throw $_
    } -Finally {
        echo " ::Cleanup resources..."
    }

相關(guān)文章

最新評論