【问题标题】:Powershell trap [Exception] is not catching my errorPowershell 陷阱 [Exception] 没有发现我的错误
【发布时间】:2011-08-26 02:11:47
【问题描述】:

由于某种原因,当我对不存在的文件运行以下脚本时,我的脚本没有捕获异常。我基于我在网上找到的示例中的代码,但它似乎对我不起作用。

如果有任何关于如何解决此问题的提示或指示,我将不胜感激。

注意:在下面的示例中我也尝试过

trap [Exception] {

但这也没用。

这是脚本:

function CheckFile($f) {

      trap {
        write-host "file not found, skipping".
        continue
      }

      $modtime = (Get-ItemProperty $f).LastWriteTime

      write-host "if file not found then shouldn't see this"
}


write-host "checking a file that does not exist"
CheckFile("C:\NotAFile")
write-host "done."

输出:

PS > .\testexception.ps1
checking a file that does not exist
Get-ItemProperty : Cannot find path 'C:\NotAFile' because it does not exist.
At C:\Users\dleclair\Documents\Visual Studio 2010\lib\testexception.ps1:12 char:35
+       $modtime = (Get-ItemProperty <<<<  $f).LastWriteTime
    + CategoryInfo          : ObjectNotFound: (C:\NotAFile:String) [Get-ItemProperty], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand

if file not found then shouldn't see this
done.
PS >

【问题讨论】:

    标签: powershell exception exception-handling powershell-trap


    【解决方案1】:

    试试这样:

    trap { write-host "file not found, skipping";continue;}
    $modtime = Get-ItemProperty c:\manoj -erroraction stop
    

    基于 OP 的 cmets:

    我认为您误解了您链接到的文章中所说的内容:

    在这个例子中,我们使用 continue 导致执行返回到 确定陷阱所在的范围并执行下一个命令。重要的是 请注意,执行只返回到陷阱的范围,所以如果 异常被抛出在函数内部,甚至在 if 语句中, 并被困在其中…… continue 将在结束时恢复 嵌套范围。

    所以如果你这样做:

    trap{ write-host $_; continue;}
    throw "blah"
    write-host after
    

    after 将被打印出来。

    但如果你这样做:

    trap{ write-host $_ ; continue}
    function fun($f) {
    
    
          throw "blah"
          write-host after
    }
    
    fun
    write-host "outside after"
    

    after 不会被打印,但outside after 会。

    或者,使用 try-catch 块:

          try{
          $modtime = (Get-ItemProperty $f -erroraction stop).LastWriteTime
          write-host "if file not found then shouldn't see this"
          }
          catch{
            write-host "file not found, skipping".
          }
    

    【讨论】:

    • 不是-erroraction stop,如果你想看到“找不到文件,正在跳过”
    • @JPBlanc - 谢谢!意味着只有停止。正在尝试使用 continue 并粘贴其他内容。
    • 我已将脚本修改为如下所示:
    • 我已将脚本修改为如下所示: function CheckFile($f) { trap { write-host "file not found, skipping" continue } $fprop = Get-ItemProperty $f -erroraction stop $modtime = $fprop.LastWriteTime write-host "如果找不到文件,则不应该看到这个,modtime = $modtime" } write-host "检查一个不存在的文件" CheckFile("C:\NotAFile")写主机“完成”。
    • 这已阻止异常被转储到屏幕上。但是,我仍然看到写着“写主机”如果找不到文件,那么不应该看到这个“被打印出来。通过阅读huddledmasses.org/trap-exception-in-powershell 的帖子,我了解到捕获异常应该导致执行仅在捕获捕获的范围之外开始。所以在这种情况下,我希望函数内部的 write-host 调用被跳过,事情会从函数外部重新开始。
    【解决方案2】:

    您必须将$ErrorActionPreference 设置为SilentlyContinue 在函数内部或脚本外部(对于全局应用程序),trap 才能工作。或者(如上所述),将-ErrorAction common 参数设置为相同的值。

    【讨论】:

      猜你喜欢
      • 2010-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      相关资源
      最近更新 更多