Benutzer:MovGP0/Powershell/Exception Handling

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      


Exception Handling

[Bearbeiten | Quelltext bearbeiten]
try {
    # ...
} catch {
    Write-Exception $_
    throw;
} finally {
    # ...
}
function Write-Exception {
  param(
    [Parameter(Mandatory=$true, Position=0)]
    $thisError
  )
  process{
    [System.Text.StringBuilder]$builder = New-Object -TypeName "System.Text.StringBuilder"; 
    $builder.AppendLine($thisError.Exception);
    $builder.AppendLine($thisError.Exception.StackTrace);
    $builder.AppendLine("at line $($thisError.InvocationInfo.ScriptLineNumber) char $($thisError.InvocationInfo.OffsetInLine)");
    $builder.AppendLine($thisError.InvocationInfo.Line);
    $builder.AppendLine($thisError.InvocationInfo.ScriptName);
    Write-Error $builder.ToString();
  }
}
function Do-Something {
    param(
        [Parameter()]
        [string]$foo
    )
    trap [Exception] {
        Write-Exception $_
        Continue # Break
    }
    begin {
    }
    process {
        # ...
    }
    end {
    }
}