'throwing more than one custom exception in powershell

I have a situation where i have to throw multiple custom exceptions in a try block in my powershell script something like below

try {
    if (!$condition1) {
        throw [MyCustomException1] "Error1"
    }
    if (!$condition2) {
        throw [MyCustomException2] "Error2"
    }
}catch [MyCustomException1] {
    #do some business logic
}catch [MyCustomException2] {
    #do some other business logic
}catch{
    #do something else
}

Is there a way to do it in powershell without writing the .net class MyCustomException1 and MyCustomException2. I don't have to store any info in the class but i just need a way to differentiate the exceptions. I could do as below but i just wondering if something cleaner.

try {
    if (!$condition1) {
        throw "Error1"
    }
    if (!$condition2) {
        throw "Error2"
    }
}catch {
    if($_.tostring() -eq "Error1"){
        Write-Host "first exception"
    }elseif($_.tostring() -eq "Error2"){
        Write-Host "Second exception"
    }else {
        Write-Host "third exception"
    }
}

Note: I have already checked below stack overflow questions: powershell-creating-a-custom-exception powershell-2-0-try-catch-how-to-access-the-exception powershell-creating-and-throwing-new-exception But it doesn't answer my question.



Solution 1:[1]

You could look at the FullyQualifiedErrorID property on the $error variable to get the string from the throw statement.

try {throw "b"}
catch {
    if ($error[0].FullyQualifiedErrorID -eq "a") {'You found error "a"'}
    if ($error[0].FullyQualifiedErrorID -eq "b") {'You found error "b"'}
}

But it looks like you don't really want to Try Catch but to make non-terminating errors. You could use Write-Error to do this.

if (!$condition1) {
    Write-Error "Error1"
}
if (!$condition2) {
    Write-Error "Error2"
}

Solution 2:[2]

You can utilize $PSCmdlet.ThrowTerminatingError and define your own ErrorRecord object.

Here's another useful article on error handling in PowerShell.

For your question on custom classes, you can also utilize your own classes in PSv5+


After reading your comments, you can figure out which errors you want to handle by using:

Catch { "[$($_.Exception.GetType().FullName)]" }

This gives you the class of error that is being thrown. Paired with:

Catch { $_.Exception.Message }

You can also see what the system is reporting.

Solution 3:[3]

I was just trying to find something really basic for error treatment and I came up with this:

try {
    $a = 1
    if($a-eq1) { throw "OneException" }
    if($a-eq2) { throw "TwoException" }
} catch {
    switch ($error[0]){
        "OneException" { echo "one" }
        "TwoException" { echo "two" }
        default { echo $_.Exception }
    }
}

Not fancy, but functional :)

Solution 4:[4]

Enable Bezu Nodes as services.

Example:

  1. Create service /vol/hbesu/node2/besu-node2.service:

    [Unit]
     Description=Besu client
     DefaultDependencies=no
     After=syslog.target network.target
     [Service]
     User=admin-besu
     Group=admin-besu
     Type=simple
     ExecStart=/usr/local/besu-21.1.0-RC1/bin/besu --data-path=/vol/hbesu/node2/data --bootnodes=enode://ae70183
     KillMode=process
     KillSignal=SIGINT
     TimeoutStopSec=90
     Restart=on-failure
     RestartSec=5s
     [Install]
     WantedBy=multi-user.target
    

sudo cp /vol/hbesu/node2/besu-node2.service /usr/lib/systemd/system/

  1. Start service:

    sudo systemctl daemon-reload

    sudo systemctl enable besu-node2.service

    sudo systemctl start besu-node2.service

    sudo systemctl status -l besu-node2.service

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BenH
Solution 2 Maximilian Burszley
Solution 3 lazy panda
Solution 4 foreston