powershell-5.0 - Powershell:如果在“try”块内引发非终止错误,则变为终止错误
问题描述
我需要一些指针来理解这种行为:
'---------------------------------'
$ErrorActionPreference = 'Continue'
function Do-Something {
Invoke-WebRequest 'http://www.notexistanthost.xyz/notexistantpath/'
'first error is non-terminating'
try
{
Invoke-WebRequest 'http://www.notexistanthost.xyz/notexistantpath/'
'never reached'
}
catch
{
'second time the same error becomes terminating and go in catch block'
}
}
Do-Something
通常返回非终止错误的函数,具有相同的参数返回在try
块内时会产生终止错误。
为什么以及何时发生这种情况?这与每个功能有关吗?
解决方案
如果它在 try/catch 中工作,则它是一个终止错误。它鲜为人知,但有两种终止错误,脚本终止和命令终止。try 看到命令终止错误并对其进行操作。'1/0' 也是命令终止。
try { 1/0; 'hi' } catch { 'caught' }
caught
“找不到路径”不是终止错误:
try { dir foo } catch { 'caught' }
Get-ChildItem: Cannot find path '/Users/js/foo' because it does not exist.
推荐阅读
- postgresql - 无法连接到 GCE 虚拟机上的我的 PostgreSQL 数据库
- c# - 如何从 CS 文件访问生成的 XAML 项
- reactjs - React:我应该使用什么来与 API 交互?
- java - 如果有新的应用版本可用,如何触发更新(来自 GITHUB)
- angular - HTTP 错误 502.5 - ANCM 进程外启动失败
- android - 离线 DataBinding = enable 导致错误:找不到 databinding-runtime.jar
- python - 在 pycharm 中获取代理错误。设置已应用但仍会出现
- java - 在 selenium 中循环一个 try 和 catch 语句
- java - 如何在 Java POJO(文本、图像、Pdf、Doc 等)中为消息指定单个属性/字段?
- jupyter-notebook - 使用 jupyter 小部件动态生成与搜索相结合的复选框