Powershell尝试,捕获:为什么我们需要最后?

0xG*_*0xG 0 powershell finally try-catch

这给我带来了一个至高无上的新手问题,但我无法在googleverse的任何地方找到答案.我写了一些相当复杂的脚本,但从未想过这个:

try { raise-errorCode}
catch { do-someThing}
finally {more-code}
Run Code Online (Sandbox Code Playgroud)

与:

try { raise-errorCode}
catch { do-someThing}
more-code
Run Code Online (Sandbox Code Playgroud)

有什么不同?

Li3*_*357 5

finally无论结果如何,都会执行一个块try/catch.在第二个示例中,more-code不保证执行.如果存在致命异常并且程序需要退出,则该finally块将始终执行,而more-code在第二个示例中则不会.