gpt4 book ai didi

powershell - 引发异常时如何设置退出代码

转载 作者:行者123 更新时间:2023-12-03 22:23:13 24 4
gpt4 key购买 nike

MyScript.ps1:

exit 1

MyThrow.ps1:
throw "test"

在PowerShell中执行:
& ".\MyScript.ps1"
Write-Host $LastExitCode # Outputs 1

Clear-Variable LastExitCode

& ".\MyThrow.ps1"
Write-Host $LastExitCode # Outputs nothing

引发异常时如何设置适当的退出代码?

最佳答案

你不知道当您引发异常时,您期望有人来处理它。有人将终止执行并设置退出代码。例如:

try {
& ".\MyThrow.ps1"
} catch {
exit 1
}

如果没有什么可以捕捉到您的异常,则不应该首先将其抛出,而应立即退出(使用正确的退出代码)。

关于powershell - 引发异常时如何设置退出代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28724388/

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