我什么时候应该使用 Write-Error 和 Throw?终止与非终止错误 [英] When should I use Write-Error vs. Throw? Terminating vs. non-terminating errors

查看:25
本文介绍了我什么时候应该使用 Write-Error 和 Throw?终止与非终止错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看 PoshCode 上的 Get-WebFile 脚本,http://poshcode.org/3226,我注意到这个对我来说很奇怪的装置:

Looking at a Get-WebFile script over on PoshCode, http://poshcode.org/3226, I noticed this strange-to-me contraption:

$URL_Format_Error = [string]"..."
Write-Error $URL_Format_Error
return

这是什么原因而不是以下原因?

What is the reason for this as opposed to the following?

$URL_Format_Error = [string]"..."
Throw $URL_Format_Error

甚至更好:

$URL_Format_Error = New-Object System.FormatException "..."
Throw $URL_Format_Error

据我所知,您应该对非终止错误使用 Write-Error,对终止错误使用 Throw,所以在我看来,您不应该使用 Write-Error 后跟 Return.有区别吗?

As I understand, you should use Write-Error for non-terminating errors, and Throw for terminating errors, so it seems to me that you should not use Write-Error followed by Return. Is there a difference?

推荐答案

Write-Error 如果要通知用户非严重错误,则应使用.默认情况下,它所做的只是在控制台上以红色文本打印错误消息.它不会阻止管道或循环继续.Throw 另一方面会产生所谓的终止错误.如果您使用 throw,管道和/或当前循环将被终止.实际上,除非您使用 traptry/catch 结构来处理终止错误,否则所有执行都将终止.

Write-Error should be used if you want to inform the user of a non-critical error. By default all it does is print an error message in red text on the console. It does not stop a pipeline or a loop from continuing. Throw on the other hand produces what is called a terminating error. If you use throw, the pipeline and/or current loop will be terminated. In fact all execution will be terminated unless you use a trap or a try/catch structure to handle the terminating error.

有一点需要注意,如果您将 $ErrorActionPreference 设置为 "Stop" 并使用 Write-Error它会产生一个终止错误.

There is one thing to note, if you set $ErrorActionPreference to "Stop" and use Write-Error it will produce a terminating error.

在您链接到的脚本中,我们发现:

In the script you linked to we find this:

if ($url.Contains("http")) {
       $request = [System.Net.HttpWebRequest]::Create($url)
}
else {
       $URL_Format_Error = [string]"Connection protocol not specified. Recommended action: Try again using protocol (for example 'http://" + $url + "') instead. Function aborting..."
       Write-Error $URL_Format_Error
    return
   }

看起来该函数的作者想要停止该函数的执行并在屏幕上显示错误消息,但不希望整个脚本停止执行.脚本作者可以使用 throw 但这意味着您在调用函数时必须使用 try/catch.

It looks like the author of that function wanted to stop the execution of that function and display an error message on screen but did not want the entire script to stop executing. The script author could have used throw however it would mean you would have to use a try/catch when calling the function.

return 将退出当前范围,可以是函数、脚本或脚本块.这最好用代码来说明:

return will exit the current scope which can be a function, script, or script block. This is best illustrated with code:

# A foreach loop.
foreach ( $i in  (1..10) ) { Write-Host $i ; if ($i -eq 5) { return } }

# A for loop.
for ($i = 1; $i -le 10; $i++) { Write-Host $i ; if ($i -eq 5) { return } }

两者的输出:

1
2
3
4
5

这里的一个问题是使用 returnForEach-Object.它不会像人们预期的那样中断处理.

One gotcha here is using return with ForEach-Object. It will not break processing like one might expect.

更多信息:

这篇关于我什么时候应该使用 Write-Error 和 Throw?终止与非终止错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆