gpt4 book ai didi

powershell - 如何从 PowerShell 中的 catch block 重新抛出异常?

转载 作者:行者123 更新时间:2023-12-02 19:08:44 26 4
gpt4 key购买 nike

我应该以与 .NET 相同的方式处理异常吗?

那么,如何在 PowerShell 中从 catch block 重新抛出异常?

抛出就足够了吗?或者抛出 $_ 会更好吗?

最佳答案

如果您想重新抛出原始异常,您可以使用 throw (最常见),或throw $_ ,或throw $_.Exception

ps:里面catch变量$_本身并不异常(exception),但是 System.Management.Automation.ErrorRecord包含异常

<小时/>

注意

throw PowerShell 中的关键字的行为与 .NET 实现不同:在 .NET 中,您只能抛出 System.Exceptions本身或其后继者,但在 PowerShell 中,您可以抛出任何内容,并且它会自动包装到 System.Management.Automation.RuntimeException 中。 。请参阅片段here .

关于powershell - 如何从 PowerShell 中的 catch block 重新抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13820140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com