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

问题描述

在PoshCode上查看Get-WebFile脚本:
我注意到了奇怪的对我来说:

  $ URL_Format_Error = [string]...
写错误$ URL_Format_Error
return

这是什么原因,而不是:

  $ URL_Format_Error = [string]...
抛出$ URL_Format_Error
pre>

甚至更好:

  $ URL_Format_Error = New-Object System.FormatException...
抛出$ URL_Format_Error

据了解,您应该对于非终止错误,使用Write-Error,并且为了终止错误而抛出,所以在我看来,你不应该使用Write-Error,而不是Return。有没有区别?

解决方案

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



如果您将 $ ErrorActionPreference 设置为 停止 ,并使用写入错误产生终止错误



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

  if($ url。包含(http)){
$ request = [System.Net.HttpWebRequest] :: Create($ url)
}
else {
$ URL_Format_Error = [string] 连接协议未指定。建议的操作:重新尝试使用协议(例如http://+ $ url +')。函数aborting ...
写入错误$ URL_Format_Error
返回
}

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



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

 #一个foreach循环。 
foreach($ i in(1..10)){Write-Host $ i; if($ i -eq 5){return}}

#A for循环。 ($ i = 0; $ i -le 10; $ i ++)
{Write-Host $ i; if($ i -eq 5){return}}

两者的输出:

  1 
2
3
4
5

这里有一个使用返回 ForEach-Object



更多信息:




  • $ ErrorActionPreference

  • try / catch

  • 陷阱

  • throw

  • 返回


Looking at a Get-WebFile script over on PoshCode: http://poshcode.org/3226I 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:

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

or even better:

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

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 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.

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
}

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 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 = 0; $i -le 10; $i++) { Write-Host $i ; if ($i -eq 5) { return } }

Output for both:

1
2
3
4
5

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

More info:

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

09-01 22:15