PowerShell的:创建一个自定义异常自定义、创建一个、异常、PowerShell

2023-09-03 04:03:18 作者:咔哇咿の箪纯

下面是我的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
}

我想替换#掷自定义异常与code,这将导致捕捉来火。我该怎么办呢?

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

推荐答案

不知道我真正得到你的问题,但似乎所有你想要做的是:

Not sure I really get your question, but it seems like all you want to do is to:

throw "message for the exception"