3

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

2 Answers 2

3

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
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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'
}

1 Comment

I will file this away for a later use. But for now, the simple answer provided by @ArcSet has proven to be very helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.