Powershell捕获块内未捕获异常的意外行为
以下powershell脚本生成一个非常未接受的输出:Powershell捕获块内未捕获异常的意外行为,powershell,uncaught-exception,catch-block,Powershell,Uncaught Exception,Catch Block,以下powershell脚本生成一个非常未接受的输出: try { throw "some exception" echo "check point" } catch { echo "catch" throw "some other exception" exit(-1) } echo "finish" 输出: catch finish 我将通过未捕获异常抛出“some exception”和脚本未到达finish退出脚本 即使Powershell设置
try {
throw "some exception"
echo "check point"
}
catch
{
echo "catch"
throw "some other exception"
exit(-1)
}
echo "finish"
输出:
catch
finish
我将通过未捕获异常抛出“some exception”
和脚本未到达finish
退出脚本
即使Powershell设置为在未捕获异常后继续,我也希望它执行exit(-1)
,并且永远不会到达finish
有人能解释这个行为吗?退出(-1)
是不可访问的,因为前一行,抛出“其他异常”
抛出一个绕过块其余部分的异常
我看到在两种情况下达到“完成”:
- 正如培根所说,当
设置为$ErrorActionPreference
时“SilentlyContinue”
- 代码被粘贴到交互式shell中(与从.ps1文件运行相反),在这种情况下,它将以手动命令的形式运行最后一行
$ErrorActionPreference
设置为SilentlyContinue?代码位于脚本文件test.ps1
中,并从shell运行脚本文件。我相信$ErrorActionPreference
默认值为Continue
我希望引发“其他一些异常”
将从脚本文件退出返回shell,而不仅仅是退出catch块。这是我回答中关于“完成”的部分。