PowerShell: Creating a Custom Exception

Viewed 14247

Here is my code:

Function Foo {
    If (1 -Eq 2) {
        # Do stuff
    }
    Else {
        # Throw custom exception
    }
}

Try {
    Foo

    Write-Host "Success"
}
Catch {
    $ErrorMessage = $_.Exception.InnerException.Message

    Write-Host "Failure"

    # Do stuff with the error message
}

I'd like to replace # Throw custom exception with code that will cause the Catch to fire. How can I do that?

1 Answers
Related