相关疑难解决方法(0)

我什么时候应该使用Write-Error vs. Throw?终止与非终止错误

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

$URL_Format_Error = [string]"..."
Write-Error $URL_Format_Error
return
Run Code Online (Sandbox Code Playgroud)

与以下相反,这是什么原因?

$URL_Format_Error = [string]"..."
Throw $URL_Format_Error
Run Code Online (Sandbox Code Playgroud)

甚至更好:

$URL_Format_Error = New-Object System.FormatException "..."
Throw $URL_Format_Error
Run Code Online (Sandbox Code Playgroud)

据我所知,你应该使用Write-Error来解决非终止错误,并使用Throw来终止错误,所以在我看来你不应该使用Write-Error然后使用Return.有区别吗?

error-handling powershell powershell-2.0

134
推荐指数
5
解决办法
14万
查看次数