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

查看:92
本文介绍了什么时候应该使用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?

推荐答案

写入错误应该使用您想通知用户非严重错误。默认情况下,它所做的只是在控制台上以红色文本显示一条错误消息。它不会阻止管道或循环继续进行。另一方面, Throw 会产生所谓的终止错误。如果使用throw,则管道和/或电流循环将终止。实际上,除非使用 trap try / 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 设置为,则需要注意一件事停止 ,并使用写入错误,它会产生终止错误

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

这里的一个陷阱是使用 return ForEach-Object

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

更多信息:

  • $ErrorActionPreference: about_Preference_Variables
  • try/catch: about_Try_Catch_Finally
  • trap: about_Trap
  • throw: about_Throw
  • return: about_Return

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

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