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
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-Errorshould 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.Throwon 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 atrapor atry/catchstructure to handle the terminating error.There is one thing to note, if you set
$ErrorActionPreferenceto"Stop"and useWrite-Errorit will produce a terminating error.In the script you linked to we find this:
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
throwhowever it would mean you would have to use atry/catchwhen calling the function.returnwill exit the current scope which can be a function, script, or script block. This is best illustrated with code:Output for both:
One gotcha here is using
returnwithForEach-Object. It will not break processing like one might expect.More information:
$ErrorActionPreference: about_Preference_Variablestry/catch: about_Try_Catch_Finallytrap: about_Trapthrow: about_Throwreturn: about_Return