PowerShell Try/Catch with If Statements

17,779

Solution 1

The Error you throw is stored at $_.Exception.Message

$a = 1
try{
    If($a -eq 1){
        throw "1"
    }
}catch{
    if ($_.Exception.Message -eq 1){
        "Error 1"
    }else{
        $_.Exception.Message
    }
}

Solution 2

I would suggest using the $PSCmdlet ThrowTerminatingError() method. Here's an example:

Function New-ErrorRecord
{
    param(
        [String]$Exception,
        [String]$ExceptionMessage,
        [System.Management.Automation.ErrorCategory] $ErrorCategory,
        [String] $TargetObject
    )

    $e = New-Object $Exception $ExceptionMessage
    $errorRecord = New-Object System.Management.Automation.ErrorRecord $e, $ErrorID, $ErrorCategory, $TargetObject
    return $ErrorRecord
}

Try
{
If (not condition)
{
    $Error = @{
      Exception = 'System.Management.Automation.ParameterBindingException'
      ExceptionMessage = 'Error text here'
      ErrorCategory = [System.Management.Automation.ErrorCategory]::InvalidArgument
      TargetObject = ''
    }
    $PSCmdlet.ThrowTerminatingError((New-ErrorRecord @Error))
}
} Catch [System.Management.Automation.ParameterBindingException]
{
    'do stuff'
}
Share:
17,779
Brandon
Author by

Brandon

Updated on June 09, 2022

Comments

  • Brandon
    Brandon almost 2 years

    Problem/Details

    I am working in PowerShell and trying to figure out how custom Try Catch statements work. My current major issue involves mixing Try/Catch and If statements. So the idea of what I am trying to achieve is something like this:

    try {
        if (!$someVariable.Text) { throw new exception 0 }
        elseif ($someVariable.Text -lt 11) { throw new exception 1 }
        elseif (!($someVariable.Text -match '[a-zA-Z\s]')) { throw new exception 2}
    }
    catch 0 {
        [System.Windows.Forms.MessageBox]::Show("Custom Error Message 1")
    }
    catch 1 {
        [System.Windows.Forms.MessageBox]::Show("Custom Error Message 2")
    }
    catch 2 {
        [System.Windows.Forms.MessageBox]::Show("Custom Error Message 3")
    }
    

    Now I know the above code is very inaccurate in terms of what the actual code will be, but I wanted to visually display what I'm thinking and trying to achieve.

    Question

    Does anyone know how to create custom error messages with PowerShell that could assist me with achieving something close to the above idea and explain your answer a bit? Thank you in advance

    So far, the link below is the closest thing I have found to what I'm trying to achieve:

    PowerShell Try, Catch, custom terminating error message

  • Brandon
    Brandon over 6 years
    Ah! Thank you. I get it now. So I can simply use throw and append anything I want in " " and then when I do my catch statement... I can then parse through those with either an if or a switch. Perfect, perfect. Thank you. I don't know how I wasn't getting that.
  • Brandon
    Brandon over 6 years
    I will file this away for a later use. But for now, the simple answer provided by @ArcSet has proven to be very helpful