【问题标题】:Powershell Catch ExceptionPowershell 捕获异常
【发布时间】:2013-06-25 04:50:57
【问题描述】:

我正在尝试这个来捕获异常,但它只是不起作用,而只是在我正在运行脚本的脚本编辑器中显示错误:-

我在脚本“\server\abc”中提到的路径并不真正存在,所以它应该将它作为一个异常捕获,但它不是。帮助帮助

Try
{

Get-ChildItem -Path "\\server\abc"

}
Catch
{

 Write-Host "error"

}

【问题讨论】:

    标签: exception powershell try-catch


    【解决方案1】:

    您需要将 erroraction 设置为 STOP 才能终止错误 - 只有终止错误才会引发到 catch 块。

    Try
    {
    
    Get-ChildItem -Path "\\server\abc" -ErrorAction Stop
    
    }
    Catch
    {
    
     Write-Host "error"
    
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 2013-09-06
    • 2016-12-17
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多