Rom*_*man 2 error-handling powershell
我正在使用Powershell并尝试使用remove-item。通常,在我的场景中该文件将不存在,并且会生成一个错误,并在我的输出中出现6行红色文本。那是非常破坏性的,在这种情况下,我不想看到任何东西。但是,如果还有其他错误,例如由于正在使用而无法删除文件,那么我想看看该错误。我以为我可以使用两个catch语句:一个带有[filenotfoundexception]或类似的东西,另一个带有没有异常类型的东西来获取其他所有内容。但是,错误文本中的任何代码都不能用作异常类型:“无法找到类型”。解决这个问题的正确方法是什么?
毫无疑问,约翰的回答非常好。
然而,我认为本次讨论中还遗漏了另一个重要的点……catch如果您能够针对已知的错误场景(例如您的错误场景)进行防御性且优雅的编程,就不要留下来做所有的工作。
$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
}
Run Code Online (Sandbox Code Playgroud)
显然,如果Remove-Item由于任何原因(例如权限或竞争条件)失败,则会抛出异常。
为确保错误进入catch块,请包含-ErrorAction Stop。
要查找错误的类型,请捕获错误并检索异常的类型:
try {
Remove-Item -Path 'c:\IDoNotExist.txt' -ErrorAction Stop
} catch {
$_.Exception.GetType().FullName
}
Run Code Online (Sandbox Code Playgroud)
要捕获特定类型的错误:
try {
Remove-Item -Path 'c:\IDoNotExist.txt' -ErrorAction Stop
} catch [System.Management.Automation.ItemNotFoundException] {
"Specific Exception Caught"
}
Run Code Online (Sandbox Code Playgroud)
未捕获的错误仍将显示为write-error正常行为的输出。
如果要捕获所有异常,但以其他方式处理其他异常,则可以包括默认捕获:
try {
Remove-Item -Path 'c:\IDoNotExist.txt' -ErrorAction Stop
} catch [System.DivideByZeroException] {
"Specific Exception Caught"
} catch {
"Generic Exception Caught"
}
Run Code Online (Sandbox Code Playgroud)
您可以为其他异常类型添加其他捕获块。如果您捕获了异常类型及其超类,则必须稍后捕获超类(否则,超类将捕获该异常,并且以后的catch块将永远不会使用)。例如,这是有效的:
try {
Remove-Item -Path 'c:\IDoNotExist.txt' -ErrorAction Stop
} catch [System.Management.Automation.ItemNotFoundException] {
"[System.Management.Automation.ItemNotFoundException]"
} catch [System.Exception] {
"[System.Exception]"
}
Run Code Online (Sandbox Code Playgroud)
...虽然不是:
try {
Remove-Item -Path 'c:\IDoNotExist.txt' -ErrorAction Stop
} catch [System.Exception] {
"[System.Exception]"
} catch [System.Management.Automation.ItemNotFoundException] {
"[System.Management.Automation.ItemNotFoundException]"
}
Run Code Online (Sandbox Code Playgroud)
终于,终于有了街区;您可能对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"
Run Code Online (Sandbox Code Playgroud)
旁注:还有一些trap用于处理异常的东西...本博客上所有这些东西的更多信息。 https://blogs.msdn.microsoft.com/kebab/2013/06/09/an-introduction-to-error-handling-in-powershell/
| 归档时间: |
|
| 查看次数: |
2486 次 |
| 最近记录: |