The Kirkland Coder: PowerShell: Try, Catch, and Finally

Sunday, February 19, 2012

PowerShell: Try, Catch, and Finally

This is the example I like to refer to for my PowerShell try/catch/finally blocks. 

###---------------------------------------------
### A try/catch/finally example.
###---------------------------------------------
try
{
    Write-Host "Try this."
    $Result = 1/$Zero
}
catch [System.DivideByZeroException]
{
    Write-Host "Caught divid by zero exception."
}

catch [System.SystemException]
{
    # This catch will catch every thing else.
    Write-Host "Caught base exception."
}
finally
{
    Write-Host "Finally this."
}


No comments:

Post a Comment