PowerShell:创建自定义异常

Nic*_*ick 7 .net powershell

这是我的代码:

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
}
Run Code Online (Sandbox Code Playgroud)

我想替换# Throw custom exception会导致Catch触发的代码.我怎样才能做到这一点?

man*_*lds 19

不确定我真的得到你的问题,但似乎你想要做的就是:

throw "message for the exception"
Run Code Online (Sandbox Code Playgroud)