Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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_Error Handling - Fatal编程技术网

Powershell 如何捕获未找到文件的错误?

Powershell 如何捕获未找到文件的错误?,powershell,error-handling,Powershell,Error Handling,我正在使用Powershell并尝试使用删除项。通常情况下,该文件不会出现在我的场景中,这将生成一个错误,并在我的输出中弹出6行红色文本。这是非常具有破坏性的,我不想在这种情况下看到任何东西。但是,如果有任何其他类型的错误,比如文件因为正在使用而无法删除,那么我希望看到该错误。 我想我可以使用两个catch语句:一个带有[filenotfoundexception]或类似的内容,另一个没有异常类型来获取其他所有内容。但是,错误文本中的任何代码都不能作为异常类型:“找不到类型”。 正确的处理方法是

我正在使用Powershell并尝试使用删除项。通常情况下,该文件不会出现在我的场景中,这将生成一个错误,并在我的输出中弹出6行红色文本。这是非常具有破坏性的,我不想在这种情况下看到任何东西。但是,如果有任何其他类型的错误,比如文件因为正在使用而无法删除,那么我希望看到该错误。 我想我可以使用两个catch语句:一个带有[filenotfoundexception]或类似的内容,另一个没有异常类型来获取其他所有内容。但是,错误文本中的任何代码都不能作为异常类型:“找不到类型”。
正确的处理方法是什么?

要确保错误进入捕捉块,请包括
-ErrorAction Stop

要查找错误的类型,请捕获错误并检索异常的类型:

try {
    Remove-Item -Path 'c:\IDoNotExist.txt' -ErrorAction Stop
} catch {
    $_.Exception.GetType().FullName
}
要捕获特定类型的错误,请执行以下操作:

try {
    Remove-Item -Path 'c:\IDoNotExist.txt' -ErrorAction Stop
} catch [System.Management.Automation.ItemNotFoundException] {
    "Specific Exception Caught"
}
未捕获的错误仍将按照正常行为显示为
写入错误
输出

PowerShell中异常的奖励信息 如果希望捕获所有异常,但以不同方式处理这些其他异常,则可以包括默认捕获:

try {
    Remove-Item -Path 'c:\IDoNotExist.txt' -ErrorAction Stop
} catch [System.DivideByZeroException] {
    "Specific Exception Caught"
} catch {
    "Generic Exception Caught"
}
您可以为其他异常类型添加其他catch块。如果您有异常类型及其超类的捕获,则超类捕获必须稍后进行(否则,超类将捕获异常,并且将永远不会使用稍后的捕获块)。e、 g.这是有效的:

try {
    Remove-Item -Path 'c:\IDoNotExist.txt' -ErrorAction Stop
} catch  [System.Management.Automation.ItemNotFoundException] {
    "[System.Management.Automation.ItemNotFoundException]"
} catch [System.Exception] {
    "[System.Exception]"
}
。。。虽然这不是:

try {
    Remove-Item -Path 'c:\IDoNotExist.txt' -ErrorAction Stop
} catch [System.Exception] {
    "[System.Exception]"
} catch  [System.Management.Automation.ItemNotFoundException] {
    "[System.Management.Automation.ItemNotFoundException]"
}
最后,还有最后一个区块;您可能熟悉C#和许多其他语言:

try {
    Remove-Item -Path 'c:\IDoNotExist.txt' -ErrorAction Stop
} catch  [System.Management.Automation.ItemNotFoundException] {
    "[System.Management.Automation.ItemNotFoundException]"
    throw #though we caught the exception we're now rethrowing it.  This is useful in scenarios such as where we want to log that an exception's occurred, but still allow the exception to bubble up to a higher layer
} catch [System.Exception] {
    "[System.Exception]"
} finally {
    "Always do this after a try/catch, regardless of whether there was an error, we caught the error, or we rethrew the error"
}
"If any uncaught error exists (i.e. including rethrown errors from the catch block) we won't reach this... If there were no errors or all errors were handled and not rethrown, we will"

旁注:还有一种叫做
trap
的方法来处理异常。。。在这个博客上有更多关于这些东西的信息

毫无疑问,约翰的回答很好

然而,我认为在这次讨论中还有另一个被忽略的要点。。。如果您可以针对已知的错误场景(如您的错误场景)进行防御性和优雅的编程,请不要让
catch
来完成所有工作


显然,如果
删除项因任何原因(例如权限或a)失败,则会引发异常。

谢谢,这是一个很好的观点;抱歉,我错过了/完全同意@Gvee的建议(以及使用本地文件名;)。
$FileToDelete = "C:\temp\NotFoundGuv.nah"

# Does the file exist?
if (Test-Path -Path $FileToDelete) {
    # Why yes it does - let's bin it off!
    Remove-Item -Path $FileToDelete
}