Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell捕获块内未捕获异常的意外行为_Powershell_Uncaught Exception_Catch Block - Fatal编程技术网

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设置

以下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块。这是我回答中关于“完成”的部分。